各种comprehension

1 什么是comprehension

list、set、dict、generator等本质上是集合。所以,数学上的集合的表示引入到python中,{x| x属于某个集合}。

所以,comprehension本质上是用来表示集合。

2 comprehension的共同特征

最前面的表达式是集合中的元素,后面的是变量的取值范围。它们二者共同决定了这个集合。

3 各个comprehension的不同点

书写方式不同而已。

4 各种comprehension

4.1 list comprehension

中括号[]

4.2 dict comprehension

大括号{}

4.3 set comprehension

大括号{}

4.4 generator comprehension

generator comprehension也是一个generator。

圆括号()

5 通用格式

前括号 x for x in 集合 if 条件 后括号

前括号 x:y for x in 集合1 for y in 集合2 if 条件 后括号

 

转载于:https://www.cnblogs.com/hustdc/p/7576020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值