python 3:列表 推导式 --- 轻量级循环

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wowocpp/article/details/86223113
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)]

猜你喜欢

转载自blog.csdn.net/wowocpp/article/details/86223113