小编Kim*_*N42的帖子

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

我是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()] 
Run Code Online (Sandbox Code Playgroud)

显然它可以工作,因为当我输入'1 2 3'并做一个print(array)输出是:

[1,2,3]

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

1 2 3

我试过做:

for i in array:
    print(array[i], end=" ")
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

2 3追溯(最近一次通话):

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

IndexError:列表索引超出范围

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

python python-3.x

21
推荐指数
5
解决办法
6万
查看次数

标签 统计

python ×1

python-3.x ×1