200526-python int list转String

在python中,可以直接通过','.join的方式来连接一个list,但是如果list中的元素不是string,会报错

1
2
3
4
5
6
>>> a = [1,2,3]
>>> ','.join(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>>

针对非string元素的列表的转换时,可以考虑借助表达式语言来处理,如下

1
2
3
4
>>> a = [1,2,3]
>>> ','.join([str(x) for x in a])
'1,2,3'
>>>
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×