今天敲小例子,報了錯TypeError: sequence item 0: expected str instance, int found
小例子
list1=[1,'two','three',4]
print(' '.join(list1))
以為會打印1 two three 4
結果報了錯
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
print(" ".join(list1))
TypeError: sequence item 0: expected str instance, int found
上網查了資料,說list包含數字,不能直接轉化成字符串。
解決辦法:
print(" ".join('%s' %id for id in list1))
即遍歷list的元素,把他轉化成字符串。這樣就能成功輸出1 two three 4結果了。
from: https://blog.csdn.net/laochu250/article/details/67649210