今天在敲代码的时候碰到这样的问题
>>> li = [1,'hello','world',2]
>>> '%s'.join(li)
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
'%s'.join(li)
TypeError: sequence item 0: expected str instance, int found
这里提示是类型错误,应该是str的类型,但是又发现了int类型
原因是列表中有整数存在,所以无法输出。
那么怎么解决呢?
>>> li = [1,'hello','world',2]
>>> ' '.join('%s'%id for id in(li)) #遍历列表中所有的整数型转换成字符
'1 hello world 2'