2

如何拆分每行的列表列表?

list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

进入:

a b c  
d e f  
g h i
4

4 回答 4

7
In [11]: lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [12]: print('\n'.join(' '.join(l) for l in lst))
a b c
d e f
g h i
于 2012-04-26T14:33:15.183 回答
5
In [1]: mylist = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: for item in mylist:
   ...:     print ' '.join(item)

a b c
d e f
g h i
于 2012-04-26T14:32:33.763 回答
5

您不想拆分元素,而是想加入它们:

>>> lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
>>> print('\n'.join(' '.join(sublist) for sublist in lst))
a b c
d e f
g h i

请注意,这list是一个可怕的变量名称,因为它掩盖了内置的list. 因此,我将变量重命名为lst.

于 2012-04-26T14:33:09.567 回答
2
myList = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
for subList in myList:
    print " ".join(subList)

(注意——不要使用保留字如listorstr来命名你的变量。这迟早会咬你)

于 2012-04-26T14:34:18.857 回答