当前位置:主页 > 软件编程 > Python代码 >

Python实现按逗号分隔列表的方法

时间:2021-07-21 08:21:13 | 栏目:Python代码 | 点击:

方法一:

def commaSpiltList(self, listData):
 listData = list(listData)
 strs = str(listData[0])
 for letter in range(1, len(listData) - 1):
 strs = strs + ',' + str(listData[letter])
 strs += ',' + str(listData[len(listData) - 1])
 print(strs)

方法二:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
L = [1,2,3,4,5]
s1 = ','.join(str(n) for n in L)
print s1

注意:字符串中替换字符的方法

s1 = ','.join(str(n) for n in listData)

您可能感兴趣的文章:

相关文章