Python :列表推导式(2019-1-15)

2019-01-26  本文已影响0人  MMatx

列表推导式

轻量级的循环创建列表

a=[x for x in range(0,4)]
print(a)
b=[x for x in range(3,10)]
print(b)
c=[x for x in range(3,23,2)]
print(c)
d=[x for x in range(3,10) if x%2==0]
print(d)
e=[x for x in range(3,10) if x%2!=0]
print(e)
f=[(x,y) for x in range(1,3) for y in range(3)]
print(f)
# 实现[1,2,3,4.......100]变成[[1,2,3],[4,5,6]...]
a=[x for x in range(1,101)]
b=[a[x:x+3] for x in range(0,len(a),3)]
print(b)
上一篇下一篇

猜你喜欢

热点阅读