您的位置:首页 > 编程语言 > Python开发

python list转换字符串报错TypeError: sequence item 0: expected str instance, int found

2017-03-28 21:16 1291 查看
今天敲小例子,报了错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结果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐