1. 程式人生 > >python 3:列表 推導式 --- 輕量級迴圈

python 3:列表 推導式 --- 輕量級迴圈

print([x for x in range(10)])
print([x*x for x in range(10)])
print([x*x for x in range(10) if x % 3 == 0 ])

print([(x,y) for x in range(3) for y in range(3)])


log:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 9, 36, 81]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]