python-二位数组(列表推导)
1、二位数组的创建
例如:
a=[i for i in range(2)]
b=[a for row in range(5)]
c=[i for i in range(2) for row in range(5)]
print(a)
print(b)
print(c)
结果:
[0, 1]
[[0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
解析:
a=[i for i in range(2)]表的的意思类似于如下代码:
for i in range(2):
print(i)
b=[a for row in range(5)]表达的意思类似于如下代码:
for row in range(5):
print(a)
c=[i for i in range(2) for row in range(5)]表达的意思类似于如下代码:
for i in range(2):
a=i
for row in renge(5):
print(a)
总结:意思就是将for前面的元素按照for后面的循环次数进行循环打印。