拆分列表列表

时间:2012-04-26 14:29:33

标签: python

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

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

成:

a b c  
d e f  
g h i

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:5)

您不想拆分元素,您想要join它们:

>>> 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

答案 3 :(得分:2)

myList = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
for subList in myList:
    print " ".join(subList)

(注意 - 请勿使用liststr等保留字来命名您的变量。这会让您早日忍受而不是更晚。)