4

如果我有三个列表,例如

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]

想这样打印

1  4  7
2  5  8
3  6  9

我该怎么做?

4

3 回答 3

8

困难的部分是转置数组。但这很容易,使用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))
于 2013-05-21T19:24:54.373 回答
6

这应该这样做:

'\n'.join(' '.join(map(str,tup)) for tup in zip(a,b,c))
于 2013-05-21T19:19:48.780 回答
2

列表理解生成器表达式,没有 map 函数:

'\n'.join(' '.join(str(y) for y in x) for x in zip(a,b,c))
于 2013-05-21T19:29:38.087 回答