推导式是Python中很强大的,很受欢迎的特性,具有语言简洁,速度快等优点.
推导式包括:1.列表(list)推导式 2.字典(dict)推导式 3.集合(set)推导式
一、列表推导式
1、使用[]生成list
例:
<1>求整除3的数字列表
- <span style="font-size:16px;">
- numbers=[]
- for x in range(100):
- if x%3==0:
- numbers.append(x)
- print(numbers)</span>
使用推导来实现
- <span style="font-size:16px;">
- numbers=[x for x in range(100) if x%3==0]
- print(numbers)</span>
执行命令得到
- <span style="font-size:16px;">
- .
- .
- .
- [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
- </span>
<2> def是一个关键字 用来声明函数
def声明函数的格式为
def 函数名(参数1,参数2..参数n):
函数体
求0-29之间三的倍数值得
- <span style="font-size:16px;">
- def squared(x):
- return x*x#返回值
- multiples=[squared(i) for i in range(30) if i % 3 is 0]
- print(multiples)</span>
执行命令得
- <span style="font-size:16px;">
- [0, 9, 36, 81, 144, 225, 324, 441, 576, 729]</span>
2、使用()生成 generator将列表推导式的[]改成()即可得到生成器。
- <span style="font-size:16px;">
- multiples=(i for i in range (30) if i % 3 is 0)
- print(type(multiples))</span>
执行命令得
- <span style="font-size:16px;">
- <class 'generator'></span>
二、字典推导式
字典推导和列表推导的使用方法是类似,只不过中括号改成了大括号.直接举例说明:
例:
<1>大小写key合并
- <span style="font-size:16px;">
- mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
- mcase_frequency = {
- k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0)
- for k in mcase.keys()
- if k.lower() in ['a','b']
- }
- print (mcase_frequency)</span>
执行命令得
- <span style="font-size:16px;">
- {'a': 17, 'b': 34}</span>
<2>快速更换key和value
- <span style="font-size:16px;">
- mcase={'a':10,'b':34}
- mcase_frequency={v:k for k,v in mcase.items()}
- print(mcase_frequency)</span>
执行命令得
- <span style="font-size:16px;">
- {10: 'a', 34: 'b'}</span>
三、集合推导
他跟列表推导式也是类似的,唯一的区别在于它使用的是花括号{}
例:
<1>求列表里的2次幂
- <span style="font-size:16px;">
- squared={x**2 for x in [1,1,2]}
- print(squared)</span>
执行命令得
- <span style="font-size:16px;">
- {1, 4}</span>
<2>使用集合推导 字符串的长度的集合
- <span style="font-size:16px;">
- strings=['a','is','with','if','file','exception']
- set={len(s) for s in strings}#有长度相同的{}会只留一个[]多个,这在实际中也非常有用
- print(set)</span>
执行命令得
- <span style="font-size:16px;">
- {1, 2, 4, 9}</span>
练习
<1>0-9的次方
- <span style="font-size:16px;">
- squares=[]
- for x in range(10):
- squares.append(x**2)
- print(squares)
- # 使用推导实现
- squares=[x**2 for x in range(10)]
- print(squares)</span>
执行命令得
- <span style="font-size:16px;">
- [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span>
<2>将单词长度大于3的转为大写输出
- <span style="font-size:16px;">
- names=['bob','tom','alice','jerry','wendy','smith']
- print([name.upper() for name in names if len(name)>3])</span>
执行命令得
- <span style="font-size:16px;">
- ['ALICE', 'JERRY', 'WENDY', 'SMITH']</span>
<3>求(x,y)其中x是0-5之间的偶数,y是0-5之间的奇数组成的元祖列表
- <span style="font-size:16px;">
- num=[(x,y) for x in range(5) if x%2==0 for y in range(5) if y%2==1]
- print(num)</span>
执行命令得
- <span style="font-size:16px;">
- [(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]</span>
<4>求m中3,6,9组成的列表
- <span style="font-size:16px;">
- m=[[1,2,3],[4,5,6],[7,8,9]]
- f=[row[2] for row in m]
- print(f)
- # 或者
- f=[m[row][2] for row in (0,1,2)]
- print(f)
- </span>
执行命令得
- <span style="font-size:16px;">
- [3, 6, 9]</span>
<5>求m中1,5,9组成的列表
- <span style="font-size:16px;">
- m=[[1,2,3],[4,5,6],[7,8,9]]
- g=[m[i][i]for i in range(len(m))]
- print(g)</span>
执行命令得
- <span style="font-size:16px;">
- [1, 5, 9]</span>
<6>求m,n中矩阵各个元素的乘积
- <span style="font-size:16px;">
- m=[[1,2,3],[4,5,6],[7,8,9]]
- n=[[2,2,2],[3,3,3],[4,4,4]]
- f=[m[row][col]*n[row][col]for row in range(3) for col in range(3)]
- g=[[m[row][col]*n[row][col]for col in range(3)] for row in range(3)]
- j=[[m[row][col]*n[row][col]for row in range(3)] for col in range(3)]
- print(f)
- print(g)
- print(j)</span>
执行命令得
- <span style="font-size:16px;">
- [2, 4, 6, 12, 15, 18, 28, 32, 36]
- [[2, 4, 6], [12, 15, 18], [28, 32, 36]]
- [[2, 12, 28], [4, 15, 32], [6, 18, 36]]</span>
<7>结合两个列表的元素,如果元素之间不相等的话生成列表
- <span style="font-size:16px;">
- combs=[]
- for x in [1,2,3]:
- for y in [3,1,4]:
- if x!=y:
- combs.append((x,y))
- print(combs)
- # 推导
- combs=[(x,y)for x in [1,2,3]for y in [3,1,4] if x!=y]
- print(combs)</span>
- <span style="font-size:16px;">执行命令得</span>
- <span style="font-size:16px;">
- [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span>
<8>创建一个列表求2次幂形式如(number,square)
- <span style="font-size:16px;">
- m=[(x,x**2) for x in range(6)]
- print(m)</span>
执行命令得
- <span style="font-size:16px;">
- [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]</span>

被折叠的 条评论
为什么被折叠?



