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

yipeiwu_com7年前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)

以上这篇Python实现按逗号分隔列表的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现倒计时小工具

本文实例为大家分享了python实现倒计时小工具的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # coding=utf-8 import th...

python笔记_将循环内容在一行输出的方法

python笔记_将循环内容在一行输出的方法

例子是输出九九乘法表 如果按照如下程序写: # 输出九九乘法表 for i in range(10): for j in range(1,i+1): print("{}...

深入分析python中整型不会溢出问题

本次分析基于 CPython 解释器,python3.x版本 在python2时代,整型有 int 类型和 long 长整型,长整型不存在溢出问题,即可以存放任意大小的整数。在pytho...

python 文件的基本操作 菜中菜功能的实例代码

python  文件的基本操作 菜中菜 文件操作 ​ open():打开 ​ file:文件的位置(路径) ​ mode:操作文件模式 &#...

Python操作MySQL数据库的方法

pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。 下载安装 pip3 install pymysql 使用操作 1、执行SQL i...