python展开列表进行打印

joh*_*sam 3 python printing format list

如果我有一个清单

lst = ['A', 'B', 'C']
Run Code Online (Sandbox Code Playgroud)

如何扩展它以便我可以打印类似的东西

print '%s %s %s' % (*lst) ?
Run Code Online (Sandbox Code Playgroud)

谢谢

Jan*_*cke 6

如果要按照概述的方式使用字符串格式,则必须事先将列表转换为元组.

>>> l = ['A', 'B', 'C']
>>> print '%s %s %s' % tuple(l)
A B C
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,我会推荐类似的东西

>>> print " ".join(l)
A B C
Run Code Online (Sandbox Code Playgroud)


mgi*_*son 6

这些天,您可以使用format:

"{} {} {}".format(*lst)  #python 2.7 and newer
"{0} {1} {2}".format(*lst) #python 2.6 and newer
Run Code Online (Sandbox Code Playgroud)