打印以空格分隔的元素列表 [英] Print a list of space-separated elements

查看:48
本文介绍了打印以空格分隔的元素列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在 Python 2 中,这可以通过以下代码轻松完成.print 语句的实现(我必须承认很神秘)避免在换行符之前打印额外的空格.

L = [1, 2, 3, 4, 5]对于 L 中的 x:打印 x,打印

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

L = [1, 2, 3, 4, 5]对于 L 中的 x:打印(x,结束=")打印()

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

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

这是一个很好的解决方案,但与 Python 2 代码相比,我发现它违反直觉,而且速度肯定更慢.另外,我知道我可以自己选择是否打印空格,例如:

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

但这又比我在 Python 2 中的还要糟糕.

那么,我的问题是,我是否在 Python 3 中遗漏了什么?print 函数是否支持我正在寻找的行为?

解决方案

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

print(*L)

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

<预><代码>>>>L = [1, 2, 3, 4, 5]>>>打印(*L)1 2 3 4 5>>>打印(*L, sep=', ')1、2、3、4、5>>>打印(*L, sep=' -> ')1 ->2 ->3 ->4 ->5

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

joined_string = ' '.join([str(v) for v in L])打印(joined_string)# 用joined_string 做其他事情

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

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).

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.

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:

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:

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.

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

解决方案

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:

>>> 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():

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!

这篇关于打印以空格分隔的元素列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆