python推导式的使用方法实例
作者:- 打小就隔路à 时间:2021-03-13 09:38:25
前言
推导式提供了更简洁高效的方法来生成序列而又不失代码的可读性。
定义: 推导式是 Python 里很有用的一个特性,它可以用一行代码就可以创建一个新的序列(比如:列表,集合,字典等等)。通过这个性能,不仅可以少写很多代码,而且性能上也更快。
Python 里有四种推导式:
列表推导式(List Comprehensions)
字典推导式(Dictionary Comprehensions)
集合推导式(Set Comprehensions)
生成器推导式(Generator Comprehensions)
一、列表推导式(List Comprehensions)
假如你有一个列表,你想去掉该列表中的所有负数,那通常的做法使用 for 循环
numbers=[1,-2,3,-4,5]
new_nums=[]
for num in numbers:
if num>0:
new_nums.append(num)
print(new_nums)
输出结果:
但如果我们用列表推导式来实现,那么这个过程就短得多,只需要一行代码:
numbers=[1,-2,3,-4,5]
new_nums=[num for num in numbers if num>0]
print(new_nums)
输出结果同上。
列表推导式的结构:
output_list = [expression for var in input_list if condition]
这里的 if condition 语句并不是必须有的。
二、字典推导式(Dictionary Comprehensions)
跟列表推导式一样,通过同样方式生成字典的方法,就是字典推导式。
假如你想从一个全是数字的列表来创建一个字典,列表里的数字作为 key, 数字的的字符串值作为 value, 然后再某个限制条件,最终获得你想要的字典数据:
nums=[1,2,3,4,5,6,7,8]
dict={}
for num in nums:
if num %2 == 0:
dict[num] = str(num)
print(dict)
输出结果:
如果我们使用字典推导式,就会简洁的多:
nums=[1,2,3,4,5,6,7,8]
dict={num:str(num) for num in nums if num % 2 == 0}
print(dict)
输出结果同上。
字典推导式的模板:
{key:value for (key,value) in dict if key,value satisfy condition}
三、集合推导式(Set Comprehensions)
集合推导式跟列表推导式差不多。就比如我们要把一个全是数字的列表中的奇数组成一个集合,用 for 循环的话:
numbers=[1,2,3,4,5,6,7,8]
filtered_nums=set()
for num in numbers:
if num %2 == 0:
filtered_nums.add(num)
print(filtered_nums)
输出结果:
如果我们使用集合表达式来表示:
numbers=[1,2,3,4,5,6,7,8]
filtered_nums={num for num in numbers if num %2 == 0}
print(filtered_nums)
输出结果同上。
注意: 集合推导式除了是使用 {} 外,其他形式都跟列表推导式一样。
四、生成器推导式(Generator Comprehensions)
生成器推导式(或叫生成器表达式),其结构也跟列表表达式相似。
例如我们将数字列表中各项平方运算并排除奇数项:
def square_even(numbers):
for number in numbers:
if number % 2 == 0:
yield (number * number)
numbers=[1,2,3,4,5,6]
square_numbers=square_even(numbers)
for number in square_numbers:
print(number)
输出结果:
使用生成器推导式显示为:
numbers=[1,2,3,4,5,6]
square_numbers=(num *num for num in numbers if num % 2 ==0)
for number in square_numbers:
print(number)
输出结果同上。
注意: 生成器推导式是使用 () ,其他基本结构跟列表推导式一样。
总结
来源:https://blog.csdn.net/weixin_54787921/article/details/114106536
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pygame 精准检测图像碰撞的问题
Python2与Python3关于字符串编码处理的差别总结
![](https://img.aspxhome.com/file/2023/1/69421_0s.png)
XML入门教程:XML CDATA的作用
numpy工程实践之np.savetxt()存储数据
![](https://img.aspxhome.com/file/2023/5/59655_0s.png)
Python字符串中查找子串小技巧
XMLHttp ASP远程获取网页内容代码
ASP和Javascript中取整函数的应用
Python中logging日志的四个等级和使用
![](https://img.aspxhome.com/file/2023/8/76688_0s.png)
Web开发的改良
详解php处理大并发大流量大存储
![](https://img.aspxhome.com/file/2023/8/555181_0s.jpg)
无组件上传图片到数据库中,asp解决方案
python机器学习Logistic回归原理推导
![](https://img.aspxhome.com/file/2023/6/76696_0s.jpg)
解析go语言调用约定多返回值实现原理
Python手机号码归属地查询代码
![](https://img.aspxhome.com/file/2023/6/67786_0s.png)
python Django模板的使用方法(图文)
![](https://img.aspxhome.com/file/2023/9/63889_0s.png)
ASP中从数据库读取二进制文件数据代码
Python获取协程返回值的四种方式详解
![](https://img.aspxhome.com/file/2023/9/62869_0s.jpg)
在ASP.NET 2.0中操作数据之三十:格式化DataList和Repeater的数据
![](https://img.aspxhome.com/file/2023/9/76339_0s.png)
python jieba分词并统计词频后输出结果到Excel和txt文档方法
![](https://img.aspxhome.com/file/2023/6/67846_0s.png)