30以内中所有能被3整除的数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
g = [i for i in range(30) if i % 3 == 0 ]#完整的列表推导式 print(g)
30以内中所有能被3整除的数的平方
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
g = [i*i for i in range(30) if i % 3 == 0 ] print(g)
找到列表中所有含有s两个字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
l =[['dsfdsa','husserhi','shdhfs','hfrues'],['sdshfhsi','sgfygs','sdfsd']] trt = [name for i in l for name in i if name.count('s') == 2] print(trt)
对于生成器而言。
只需要把中括号改为小括号就行。、
字典推导式
将一个key和value对调
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d = {'a':'23','d':'12'} d_c = {d[k]:k for k in d} print(d_c)
合并大小写对应的value值,并将key统一成小写
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
d = {'a':23,'d':12,'A':234,'G':123} f = {k.lower():d.get(k.lower,0) + d.get(k.upper(),0) for k in d} print(f)
集合推导式(集合特点去重)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
g = {l*l for l in [12,34,23,2,3,23,54,4,4,4,4,4,4]} print(g)
如果是列表推导式,则没有去重功能。会全部打印出来