Python - 打印出用逗号分隔的列表 [英] Python - printing out list separated with comma
问题描述
我正在编写一段代码,它应该输出一个用逗号分隔的项目列表.该列表是使用 for 循环生成的.我使用
for x in range(5):打印(x,结束=,")
问题是我不知道如何去掉与列表中最后一个条目一起添加的最后一个逗号.它输出这个:
0,1,2,3,4,
如何删除结尾 ' , ' ?
将 sep=","
作为参数传递给 print()
打印语句就快完成了.
不需要循环,print 有一个 sep
参数以及 end
.
一点解释
print
内置函数将任意数量的项目作为要打印的参数.任何非关键字参数都将被打印出来,以 sep
分隔.sep
的默认值是一个空格.
改变 sep
有预期的结果.
每个参数都像 str()
一样被字符串化.将可迭代对象传递给打印语句会将可迭代对象字符串化为一个参数.
但是,如果您将星号放在可迭代对象前面,则会将其分解为单独的参数,并允许按预期使用 sep
.
使用 join
作为替代
将可迭代对象连接到具有给定分隔符的字符串的另一种方法是使用分隔符字符串的 join
方法.
>>>print("残酷的".join(["hello", "world"]))你好残酷的世界
这有点笨拙,因为它需要将非字符串元素显式转换为字符串.
>>>print(",".join([str(i) for i in range(5)]))0,1,2,3,4
蛮力 - 非蟒蛇
您建议的方法是使用循环来连接字符串,并在此过程中添加逗号.当然,这会产生正确的结果,但工作量更大.
>>>>iterable = range(5)>>>结果 = "">>>对于项目,i 在 enumerate(iterable) 中:>>>结果 = 结果 + str(item)>>>如果我>len(可迭代) - 1:>>>结果=结果+,">>>打印(结果)0,1,2,3,4
I am writing a piece of code that should output a list of items separated with a comma. The list is generated with a for loop. I use the
for x in range(5):
print(x, end=",")
The problem is I don't know how to get rid of the last comma that is added with the last entry in the list. It outputs this:
0,1,2,3,4,
How do I remove the ending ' , ' ?
Pass sep=","
as an argument to print()
You are nearly there with the print statement.
There is no need for a loop, print has a sep
parameter as well as end
.
>>> print(*range(5), sep=", ")
0, 1, 2, 3, 4
A little explanation
The print
builtin takes any number of items as arguments to be printed. Any non-keyword arguments will be printed, separated by sep
. The default value for sep
is a single space.
>>> print("hello", "world")
hello world
Changing sep
has the expected result.
>>> print("hello", "world", sep=" cruel ")
hello cruel world
Each argument is stringified as with str()
. Passing an iterable to the print statement will stringify the iterable as one argument.
>>> print(["hello", "world"], sep=" cruel ")
['hello', 'world']
However, if you put the asterisk in front of your iterable this decomposes it into separate arguments and allows for the intended use of sep
.
>>> print(*["hello", "world"], sep=" cruel ")
hello cruel world
>>> print(*range(5), sep="---")
0---1---2---3---4
Using join
as an alternative
The alternative approach for joining an iterable into a string with a given separator is to use the join
method of a separator string.
>>>print(" cruel ".join(["hello", "world"]))
hello cruel world
This is slightly clumsier because it requires non-string elements to be explicitly converted to strings.
>>>print(",".join([str(i) for i in range(5)]))
0,1,2,3,4
Brute force - non-pythonic
The approach you suggest is one where a loop is used to concatenate a string adding commas along the way. Of course this produces the correct result but its much harder work.
>>>iterable = range(5)
>>>result = ""
>>>for item, i in enumerate(iterable):
>>> result = result + str(item)
>>> if i > len(iterable) - 1:
>>> result = result + ","
>>>print(result)
0,1,2,3,4
这篇关于Python - 打印出用逗号分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!