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


今天敲小例子,報了錯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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM