技术文摘
18 个 Python 列表推导式条件过滤实例
18 个 Python 列表推导式条件过滤实例
在 Python 编程中,列表推导式是一种简洁而强大的工具,能够帮助我们快速创建和处理列表。其中,条件过滤更是让我们能够从列表中筛选出符合特定条件的元素。下面将为您介绍 18 个实用的 Python 列表推导式条件过滤实例。
实例 1:筛选出大于 5 的数字
numbers = [2, 7, 3, 8, 1, 6]
filtered_numbers = [num for num in numbers if num > 5]
实例 2:筛选出偶数
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in numbers if num % 2 == 0]
实例 3:筛选出包含特定字符的字符串
strings = ["apple", "banana", "cherry", "date"]
filtered_strings = [s for s in strings if "a" in s]
实例 4:筛选出长度大于 5 的字符串
strings = ["hello", "world", "python", "code"]
long_strings = [s for s in strings if len(s) > 5]
实例 5:筛选出价格大于 100 的商品
products = [{"name": "phone", "price": 800}, {"name": "book", "price": 50}, {"name": "laptop", "price": 1200}]
expensive_products = [p for p in products if p["price"] > 100]
实例 6:筛选出以特定字母开头的单词
words = ["apple", "banana", "cherry", "date"]
filtered_words = [word for word in words if word.startswith("a")]
实例 7:筛选出年龄大于 18 岁的用户
users = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 15}, {"name": "Charlie", "age": 30}]
adult_users = [u for u in users if u["age"] > 18]
实例 8:筛选出成绩大于 80 分的学生
students = [{"name": "David", "score": 90}, {"name": "Emma", "score": 70}, {"name": "Frank", "score": 85}]
good_students = [s for s in students if s["score"] > 80]
实例 9:筛选出正整数
numbers = [-2, 5, -3, 8, 0, 1]
positive_numbers = [num for num in numbers if num > 0]
实例 10:筛选出能被 3 整除的数字
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
divisible_by_3 = [num for num in numbers if num % 3 == 0]
实例 11:筛选出包含大写字母的字符串
strings = ["hello", "World", "python", "Code"]
filtered_strings = [s for s in strings if any(c.isupper() for c in s)]
实例 12:筛选出价格在 50 到 100 之间的商品
products = [{"name": "phone", "price": 80}, {"name": "book", "price": 60}, {"name": "laptop", "price": 120}]
moderate_price_products = [p for p in products if 50 <= p["price"] <= 100]
实例 13:筛选出长度为偶数的字符串
strings = ["apple", "banana", "cherry", "date"]
even_length_strings = [s for s in strings if len(s) % 2 == 0]
实例 14:筛选出名字以字母“C”开头的用户
users = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 15}, {"name": "Charlie", "age": 30}]
c_users = [u for u in users if u["name"].startswith("C")]
实例 15:筛选出成绩在 70 到 90 分之间的学生
students = [{"name": "David", "score": 85}, {"name": "Emma", "score": 75}, {"name": "Frank", "score": 95}]
average_students = [s for s in students if 70 <= s["score"] <= 90]
实例 16:筛选出奇数
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = [num for num in numbers if num % 2!= 0]
实例 17:筛选出不包含特定子字符串的字符串
strings = ["apple", "banana", "cherry", "date"]
filtered_strings = [s for s in strings if "erry" not in s]
实例 18:筛选出价格大于平均价格的商品
products = [{"name": "phone", "price": 800}, {"name": "book", "price": 50}, {"name": "laptop", "price": 1200}]
average_price = sum(p["price"] for p in products) / len(products)
expensive_products_above_avg = [p for p in products if p["price"] > average_price]
通过以上 18 个实例,相信您对 Python 列表推导式的条件过滤有了更深入的理解和应用能力。在实际编程中,灵活运用列表推导式可以大大提高代码的简洁性和效率。
TAGS: Python 编程 实例展示 Python 列表推导式 条件过滤技巧
- Win11 绕过 TPM 验证的方法:任意版本更新均可
- Win11 右键使用时闪屏的解决之道
- Win11蓝牙功能位置及驱动方法详解
- 联想升级 Win11 触摸板失灵的解决办法
- Win11 升级后界面无变化的原因及解决教程
- Win11 任务栏全透明的设置方法
- Win11 系统删除本地用户的教程与方法
- 检测电脑能否支持Win11系统及无法运行的解决办法
- 如何通过控制面板删除 Microsoft 帐户
- 不想体验 Win11 测试版怎样退回 Win10 系统
- Win11 华硕笔记本无法打开 edge 浏览器如何解决?
- Win11 如何使用 IE 及设置浏览器兼容 IE
- Win11 系统激活状态的查看方式
- Win11 虚拟桌面的使用方法
- WinX 菜单的定制与打开方法