如果我有三个列表,例如
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
想这样打印
1 4 7
2 5 8
3 6 9
我该怎么做?
困难的部分是转置数组。但这很容易,使用zip
:
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
t = zip(a, b, c)
现在您只需将其打印出来:
print('\n'.join(' '.join(map(str, row)) for row in t))
这应该这样做:
'\n'.join(' '.join(map(str,tup)) for tup in zip(a,b,c))
和列表理解生成器表达式,没有 map 函数:
'\n'.join(' '.join(str(y) for y in x) for x in zip(a,b,c))