在使用列表转字符串中出现的错误,暂时发现了以下两种
- TypeError: sequence item 0: expected str instance, int found
- TypeError: sequence item 0: expected str instance, list found
举个栗子
-
a = [ 1,‘two’,‘three’,4]
-
print(’,’.join(a))
-
#>>
-
‘’’
-
Traceback (most recent call last):
File “C:/Users/Administrator/Desktop/onetest.py”, line 2, in
print(’,’.join(a))
TypeError: sequence item 0: expected str instance, int found -
这个错误是说,a[0]是数字
-
‘’’
-
b = [[‘g’,‘b’,‘f’],[‘r’,‘h’,‘e’]]
-
print(’,’.join(b))
-
#>>
-
‘’’
-
Traceback (most recent call last):
File “d:/Documents/meiz/pythonLianxi/NO10/lianxi1.py”, line 6, in
content = ‘,’.join(b)
TypeError: sequence item 0: expected str instance, list found -
这个错误是说,列表b[0]是列表,
-
‘’’
解决方法:
- print(’,’.join(’%s’ %i for i in a))
- #>>1,two,three,4
- print(’,’.join(’%s’ %i for i in b ))
- #>>[‘g’,‘b’,‘f’],[‘r’,‘h’,‘e’]
‘%s’ %i for i in b
遍历b列表,将b列表中的元素格式化为string