1.列表推导式书写形式:
[表达式 for 变量 in 列表] 或者 [表达式 for 变量 in 列表 if 条件]
2.举例说明:
复制代码 代码如下:
li = [1,2,3,4,5,6,7,8,9]
print [x**2 for x in li]
print [x**2 for x in li if x>5]
print dict([(x,x*10) for x in li])
print [ (x, y) for x in range(10) if x % 2 if x > 3 for y in range(10) if y > 7 if y != 8 ]
vec=[2,4,6]
vec2=[4,3,-9]
sq = [vec[i]+vec2[i] for i in range(len(vec))]
print sq
print [x*y for x in [1,2,3] for y in [1,2,3]]
testList = [1,2,3,4]
def mul2(x):
return x*2
print [mul2(i) for i in testList]
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8