[已解決]報錯: TypeError: sequence item 0: expected str instance, int found

今天敲小例子,報了錯TypeError: sequence item 0: expected str instance, int foundpython

小例子shell

list1=[1,'two','three',4]
print(' '.join(list1))

覺得會打印1 two three 4.net

結果報了錯code

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包含數字,不能直接轉化成字符串。blog

解決辦法:three

print(" ".join('%s' %id for id in list1))

即遍歷list的元素,把他轉化成字符串。這樣就能成功輸出1 two three 4結果了。字符串

from: https://blog.csdn.net/laochu250/article/details/67649210get

相關文章
相關標籤/搜索