将整数以逗号分隔的字符串加入pythonic方式

buh*_*htz 0 python python-3.x

我经常这样做:

>>> x = [1,2,3,4,5]
>>> s = ''
>>> for i in x:
...  s = '{}, {}'.format(s, i)
... 
>>> s
', 1, 2, 3, 4, 5'
>>> if s[0] == ',':
...  s = s[2:]
... 
>>> s
'1, 2, 3, 4, 5'
Run Code Online (Sandbox Code Playgroud)

我经常认为有更多的pythonic-3方法可以做到这一点.任何的想法?

gli*_*dud 6

str有一个join内置来处理这个:

', '.join(str(_) for _ in x)
Run Code Online (Sandbox Code Playgroud)