不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2020-11-04 14:37:26  修改时间:2024-03-29 02:31:31  分类:Python基础  编辑

Python中使用.join()连接list时,出现类型错误的解决办法:

>>> ls = [1,2,3]
>>> print ','.join(ls)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found

解决办法:

对list中的元素进行类型转换到string

 

>>> print ','.join('%s' % id for id in ls)
1,2,3