技术文摘
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 列表推导式 条件过滤技巧
- 函数计算异步任务能力中的任务状态与生命周期管理解密
- 与驱动编译有关的三类文件:Makefile、Config 及 Kconfig
- 系统架构设计中数据模型的选型困境
- 实用指南:四种方法助你轻松打造交互式仪表板
- Pythoner 必备的自动化利器!
- 项目日志记录,一个注解即可搞定
- RabbitMQ 解决分布式事务的方法
- 告别 if else!这三种设计模式让代码优化轻而易举!
- Node_modules 亟需整治
- 对增长趋势超越 Vite 的 TailwindCSS 进行客观评价
- ArrayList 初始化容量大小为何为 10 之奇思
- 众多 SpringBoot 开发者缘何弃 Tomcat 选 Undertow
- 解析八种架构模式
- 你不适合事件驱动架构,快醒醒
- Java 多线程中 Lock 锁的运用