在一行中打印一个 int 列表 python3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37625208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:40:38  来源:igfitidea点击:

Printing an int list in a single line python3

pythonpython-3.x

提问by KimioN42

I'm new to python and I'm trying to scan multiple numbers separated by spaces (let's assume '1 2 3' as an example) in a single line and add it to a list of int. I did it by using:

我是 python 的新手,我正在尝试在一行中扫描由空格分隔的多个数字(假设以“1 2 3”为例)并将其添加到 int 列表中。我是通过使用做到的:

#gets the string 
string = input('Input numbers: ') 
#converts the string into an array of int, excluding the whitespaces
array = [int(s) for s in string.split()] 

Apparently it works, since when I type in '1 2 3' and do a print(array)the output is:

显然它有效,因为当我输入“1 2 3”并执行print(array)输出时:

[1, 2, 3]

[1, 2, 3]

But I want to print it in a single line without the brackets, and with a space in between the numbers, like this:

但我想在没有括号的情况下将其打印在一行中,并且数字之间有一个空格,如下所示:

1 2 3

1 2 3

I've tried doing:

我试过这样做:

for i in array:
    print(array[i], end=" ")

But I get an error:

但我收到一个错误:

2 3 Traceback (most recent call last):

print(array[i], end=" ")

IndexError: list index out of range

2 3 回溯(最近一次调用最后一次):

打印(数组[i],结束=“”)

IndexError:列表索引超出范围

How can I print the list of ints (assuming my first two lines of code are right) in a single line, and without the brackets and commas?

如何在一行中打印整数列表(假设我的前两行代码是正确的),并且没有括号和逗号?

回答by Nikhil Gupta

Yes that is possible in Python 3, just use *before the variable like:

是的,这在Python 3中是可能的,只需*在变量之前使用,例如:

print(*list)

This will print the list separated by spaces.

这将打印由空格分隔的列表。

(where *is the unpackingoperator that turns a list into positional arguments, print(*[1,2,3])is the same as print(1,2,3), see also What does the star operator mean, in a function call?)

(将列表转换为位置参数*解包运算符在哪里,print(*[1,2,3])与 相同print(1,2,3),另请参阅函数调用中星号运算符的含义是什么?

回答by Nick Matteo

You want to say

你想说

for i in array:
    print(i, end=" ")

The syntax i in arrayiterates over each member of the list. So, array[i]was trying to access array[1], array[2], and array[3], but the last of these is out of bounds (arrayhas indices 0, 1, and 2).

语法i in array遍历列表的每个成员。因此,array[i]试图访问array[1],array[2]array[3],但最后一个超出范围(array索引为 0、1 和 2)。

You can get the same effect with print(" ".join(map(str,array))).

您可以使用 获得相同的效果print(" ".join(map(str,array)))

回答by Corey Goldberg

these will both work in Python 2.7 and Python 3.x:

这些都适用于 Python 2.7 和 Python 3.x:

>>> l = [1, 2, 3]
>>> print(' '.join(str(x) for x in l))
1 2 3
>>> print(' '.join(map(str, l)))
1 2 3

btw, arrayis a reserved word in Python.

btw,array是 Python 中的保留字。

回答by EoinS

Try using join on a str conversion of your ints:

尝试在 int 的 str 转换上使用 join:

print ' '.join(str(x) for x in array)

回答by 2Cubed

You have multiple options, each with different general use cases.

您有多个选项,每个选项都有不同的一般用例。

The first would be to use a forloop, as you described, but in the following way.

第一种方法是使用for循环,如您所描述的那样,但方式如下。

for value in array:
    print(value, end=' ')

You could also use str.joinfor a simple, readable one-liner using comprehension. This method would be good for storing this value to a variable.

您还可以str.join使用理解将其用于简单、可读的单行。这种方法非常适合将此值存储到变量中。

print(' '.join(str(value) for value in array))

My favorite method, however, would be to pass arrayas *args, with a sepof ' '. Note, however, that this method will only produce a printed output, not a value that may be stored to a variable.

然而,我最喜欢的方法是将arrayas传递*args给 a sepof ' '。但是请注意,此方法只会产生printed 输出,而不是可能存储到变量中的值。

print(*array, sep=' ')

回答by Somesh Samadder

# Print In One Line Python

print('Enter Value')

n = int(input())

print(*range(1, n+1), sep="")

回答by mehboob shaikh

If you write

如果你写

a = [1, 2, 3, 4, 5]
print(*a, sep = ',')

You get this output: 1,2,3,4,5

你得到这个输出: 1,2,3,4,5

回答by elad silver

For python 2.7 another trick is:

对于python 2.7,另一个技巧是:

arr = [1,2,3]
for num in arr:
  print num,
# will print 1 2 3