在python 2.7中将列表转换为字符串

use*_*677 3 python

我有一个列表 xline。它被初始化,然后被附加。但是我运行时出现错误?

xline = []
---#append
----
print (''.join(xline)) # Convert list into string
Run Code Online (Sandbox Code Playgroud)

运行时错误

    print (''.join(xline)) # Convert list into string
TypeError: sequence item 0: expected string, int found
Run Code Online (Sandbox Code Playgroud)

怎么了?

dur*_*rsk 5

您可以使用str()来转换每个元素:

print ''.join([str(x) for x in xline])
Run Code Online (Sandbox Code Playgroud)