将列表转换为字符串

时间:2014-02-21 10:16:49

标签: python list formatting indexing tuples

我有这个清单:

scores = [
    ('WillyCaballero', '2'),
    ('Angeleri', '2'),
    ('Antunes', '2'),
    ('FlavioFerreira', '2'),
    ('Camacho', '2'),
    ('SamuGarc\xc3\xada', '2'),
    ('I\xc3\xb1igoMart\xc3\xadnez', '2'),
    ('Jos\xc3\xa9\xc3\x81ngel', '6')
    ...
]

如何在一个str变量和输出中存储这样的格式?:

Willy Caballero 2   
Angeleri 2  
Antunes 2  
...

5 个答案:

答案 0 :(得分:6)

使用str.join,首先加入whitespace元素,然后加入'\n'

In [25]: print '\n'.join(' '.join(s) for s in scores)
Willy Caballero 2
Angeleri 2
Antunes 2
Flavio Ferreira 2
...

答案 1 :(得分:0)

str = '\n'.join(' '.join(s) for s in scores)
print(str)

答案 2 :(得分:0)

for i in scores:
    print i[0],i[1]

答案 3 :(得分:0)

<强>被修改

>>> out = map(' '.join, p)
>>> for i in out:
...     print i
... 
WillyCaballero 2
Angeleri 2
Antunes 2
>>> 

答案 4 :(得分:0)

我会这样做;

scoresList = ''
for score in scores:
   scoresList += ' '.join(score) + '\n'
#end loop