我有一个这样的列表:
li = [1, 2, 3, 4, 5]
我想把它变成一个字符串,去掉引号,去掉逗号,这样它看起来像这样:
1 2 3 4 5
我尝试了以下方法:
new_list = []
new_list.append(li)
new_string = " ".join(new_list)
print new_string
但是我收到以下错误:
TypeError: sequence item 0: expected str instance, int found
为什么会发生这种情况以及如何解决这个问题以获得我想要的输出?
最佳答案
列表中的项目必须是 str
类型,以便使用给定的分隔符将它们连接起来。试试这个:
' '.join(map(str, your_list)) # join the resulting iterable of strings, after casting ints
关于python - 将列表更改为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31766411/