Python,打印分隔列表 [英] Python, print delimited list
本文介绍了Python,打印分隔列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这个用于打印逗号分隔值列表的 Python 代码
Consider this Python code for printing a list of comma separated values
for element in list:
print element + ",",
如果element
是列表中的最后一个元素,那么打印时不出现逗号的首选方法是什么.
What is the preferred method for printing such that a comma does not appear if element
is the final element in the list.
前
a = [1, 2, 3]
for element in a
print str(element) +",",
output
1,2,3,
desired
1,2,3
推荐答案
>>> ','.join(map(str,a))
'1,2,3'
这篇关于Python,打印分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文