python - 打印列表元素时如何删除逗号(,)?
问题描述
我想使用for
循环打印用逗号分隔的列表的各个元素。
numbers = ['23','44','76','89']
for i in numbers:
print(i, end=",")
我看到这样的输出:
23,44,76,89,
如何删除最后一个逗号?
解决方案
numbers = ['23','44','76','89']
print(*numbers, sep=",")
# Only for printing other wise you can use join function
输出
23,44,76,89
带for循环
numbers = ['23','44','76','89']
for i,v in enumerate(numbers):
if i != len(numbers)-1:
end = ','
else:
end = ''
print(v,end=end)
推荐阅读
- symfony - 从工作人员发送电子邮件时,除非引发异常,否则不会发送
- mysql - 从具有不同信息的 3 个表中获取数据
- reactjs - React 功能组件状态变化不会触发子组件重新读取其 props
- php - 根据PHP更改所有页面上的链接
- swift - AppStore 查看崩溃日志
- cassandra - 我可以将计数器类型字段用作 C* 表的主键吗?
- r - 如何解决错误:无法在 RStudio 中分配大小为 70.7 Gb 的向量?
- python-3.x - ITM(爱尔兰横坐标)转换为谷歌地图 Python3 的 GPS
- embedded - 在 Eclipse IDE 上使用带有 MBED 库的矢量偏移表
- gremlin - 如何使用 Gremlin 查询获取特定标签的源顶点和目标顶点之间的所有传入中间顶点的列表?