采用列表生成式,代码如下:
L = ['tang','yu','di',123]
s = ','.join([str(n) for n in L])# [n for n in L]为列表生成式
s
错误原因:之前将str 声明为变量,如:str = ‘tang’,由于str() 是Python的内置函数,所以不能声明变量。
解决办法:删除str变量,重新restart,再执行代码片段,成功输出。