[英]How to append an array of space-separated integers input in an array in python?
I want to append user input of space-separated integers, as integers not array, into a formed array.我想将 append 用户输入的空格分隔的整数,作为整数而不是数组,放入一个形成的数组中。 Is there a way to do this?有没有办法做到这一点?
Here is a pseudo-code:这是一个伪代码:
a=[1,2,3,4]
a.append(int(input().split())
print(a)
I want it to be time-efficient, this is what I tried:我希望它具有时间效率,这就是我尝试过的:
a=[1,2,3,4]
b=list(map(int, input().rstrip().split()))
a.extend(b)
print(a)
Is there a more efficient / faster way?有没有更有效/更快的方法?
Expected output:预期 output:
[1, 2, 3, 4, 5, 6, 7, 8]
# When input is '5 6 7 8'
You can do so:你可以这样做:
a=[1,2,3,4]
a.extend(map(int, input().split()))
print(a)
#[1, 2, 3, 4, 5, 6, 7, 8]
You can also do it as:你也可以这样做:
a=[1,2,3,4]
b=list(map(int, input().rstrip().split()))
for i in b:
a.append(i)
print(a)
You can do so by joining two lists using '+' operator -您可以通过使用“+”运算符加入两个列表来做到这一点 -
a = [1,2,3,4]
result = list(map(int, input().split())) + a
[5, 6, 7, 8, 1, 2, 3, 4] [5、6、7、8、1、2、3、4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.