技术文摘
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 列表推导式 条件过滤技巧
- 如何解决PHP Redis数据丢失问题
- 如何定义MySQL联合查询
- SpringBoot整合Redis操作API的实现方式
- Golang 实现 MySQL 操作的方法
- SpringBoot整合Redis实现@Cacheable与RedisTemplate的使用
- MySQL 二进制包使用实例剖析
- 如何借助Systemd编译Mysql5.7.11
- 如何在Linux系统中彻底卸载MySQL
- 如何理解Linux系统连接Redis的命令
- Redis单节点实例剖析
- 在Linux系统里怎样迁移MySQL数据库
- 在SpringBoot里怎样将Redis用作全局锁
- Python操作MySQL:从数据库读取图片的方法
- MySQL 中 from_unixtime 时间戳格式化函数的使用方法
- 如何运用Mysql管理关系型数据库