在Python 3中打印空格分隔的元素列表

时间:2022-04-15 03:34:36

I have a list L of elements, say natural numbers. I want to print them in one line with a single space as a separator. But I don't want a space after the last element of the list (or before the first).

我有一个列表L元素,比如自然数。我想用一个空格作为分隔符将它们打印在一行中。但是我不希望在列表的最后一个元素之后(或者在第一个元素之前)有空格。

In Python 2, this can easily be done with the following code. The implementation of the print statement (mysteriously, I must confess) avoids to print an extra space before the newline.

在Python 2中,可以使用以下代码轻松完成此操作。 print语句的实现(神秘地,我必须承认)避免在换行符之前打印额外的空格。

L = [1, 2, 3, 4, 5]
for x in L:
    print x,
print

However, in Python 3 it seems that the (supposedly) equivalent code using the print function produces a space after the last number:

但是,在Python 3中,使用print函数的(假设的)等效代码似乎在最后一个数字后产生一个空格:

L = [1, 2, 3, 4, 5]
for x in L:
    print(x, end=" ")
print()

Of course there are easy answers to my question. I know I can use string concatenation:

当然,我的问题很简单。我知道我可以使用字符串连接:

L = [1, 2, 3, 4, 5]
print(" ".join(str(x) for x in L))

This is a quite good solution, but compared to the Python 2 code I find it counter-intuitive and definitely slower. Also, I know I can choose whether to print a space or not myself, like:

这是一个非常好的解决方案,但与Python 2代码相比,我发现它反直觉并且肯定更慢。另外,我知道我可以选择是否打印空间,例如:

L = [1, 2, 3, 4, 5]
for i, x in enumerate(L):
    print(" " if i>0 else "", x, sep="", end="")
print()

but again this is worse than what I had in Python 2.

但这再次比我在Python 2中所做的更糟糕。

So, my question is, am I missing something in Python 3? Is the behavior I'm looking for supported by the print function?

所以,我的问题是,我在Python 3中遗漏了什么?我正在寻找的行为是否受打印功能的支持?

2 个解决方案

#1


61  

You can apply the list as separate arguments:

您可以将列表应用为单独的参数:

print(*L)

and let print() take care of converting each element to a string. You can, as always, control the separator by setting the sep keyword argument:

让print()负责将每个元素转换为字符串。您可以像往常一样通过设置sep关键字参数来控制分隔符:

>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5

Unless you need the joined string for something else, this is the easiest method. Otherwise, use str.join():

除非您需要连接字符串用于其他内容,否则这是最简单的方法。否则,使用str.join():

joined_string = ' '.join([str(v) for v in L])
print(joined_string)
# do other things with joined_string

Note that this requires manual conversion to strings for any non-string values in L!

请注意,这需要手动转换为L中任何非字符串值的字符串!

#2


0  

list = [1, 2, 3, 4, 5]
for i in list[0:-1]:
    print(i, end=', ')
print(list[-1])

do for loops really take that much longer to run?

for for循环真的需要更长的时间来运行吗?

was trying to make something that printed all str values in a list separated by commas, inserting "and" before the last entry and came up with this:

我试图制作一些东西,用逗号分隔列表中的所有str值,在最后一个条目之前插入“和”,然后想出这个:

spam = ['apples', 'bananas', 'tofu', 'cats']
for i in spam[0:-1]:
    print(i, end=', ')
print('and ' + spam[-1])

#1


61  

You can apply the list as separate arguments:

您可以将列表应用为单独的参数:

print(*L)

and let print() take care of converting each element to a string. You can, as always, control the separator by setting the sep keyword argument:

让print()负责将每个元素转换为字符串。您可以像往常一样通过设置sep关键字参数来控制分隔符:

>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5

Unless you need the joined string for something else, this is the easiest method. Otherwise, use str.join():

除非您需要连接字符串用于其他内容,否则这是最简单的方法。否则,使用str.join():

joined_string = ' '.join([str(v) for v in L])
print(joined_string)
# do other things with joined_string

Note that this requires manual conversion to strings for any non-string values in L!

请注意,这需要手动转换为L中任何非字符串值的字符串!

#2


0  

list = [1, 2, 3, 4, 5]
for i in list[0:-1]:
    print(i, end=', ')
print(list[-1])

do for loops really take that much longer to run?

for for循环真的需要更长的时间来运行吗?

was trying to make something that printed all str values in a list separated by commas, inserting "and" before the last entry and came up with this:

我试图制作一些东西,用逗号分隔列表中的所有str值,在最后一个条目之前插入“和”,然后想出这个:

spam = ['apples', 'bananas', 'tofu', 'cats']
for i in spam[0:-1]:
    print(i, end=', ')
print('and ' + spam[-1])