Sah*_*and -5 python string list
我想转这个清单:
['1','2','3','4']
Run Code Online (Sandbox Code Playgroud)
进入这个字符串:
"%(1)s, %(2)s, %(3)s, %(4)s"
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,最好是在一线?
使用字符串格式化,str.join()以及列表理解:
', '.join(['%({})s'.format(i) for i in inputlist])
Run Code Online (Sandbox Code Playgroud)
演示:
>>> inputlist = ['1','2','3','4']
>>> ', '.join(['%({})s'.format(i) for i in inputlist])
'%(1)s, %(2)s, %(3)s, %(4)s'
Run Code Online (Sandbox Code Playgroud)
请参阅Python中没有[]的列表理解为什么最好使用列表推导,而不是这里的生成器表达式.