技术文摘
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 列表推导式 条件过滤技巧
- 大龄码农何去何从:35 - 40 岁的软件开发工程师陷入困境?
- 7.1 万 Star !CSS 库拥有超实用的 60 多种动画效果
- Rollup 快速上手与配置文件解析
- Web3:未来去中心化互联网的阐释
- 代码化架构守护:架构文档化作测试
- 一日一技:正则表达式中小括号的双重含义
- Java 虚引用为何令人心疼
- 微信开放接口 getUserInfo、login、getUserProfile 的复杂关系
- 深度剖析官方博客:React18已至
- TensorFlow2 识别验证码的使用教程
- React17 升级后 Toast 组件无法使用,大佬求解
- Java17 新特性已定,Java 之父:25 年漏洞终告别
- 前端百题之从验证点至手撕 New 操作符
- Python 接收邮件的多样方式
- 深入学习 unary 方法,一篇指南