[英]How to take input in this form in python?
So, I want to take input of the following -所以,我想输入以下内容 -
3
1 2
2 4
3 4
The first line contains an integer n.第一行包含一个整数 n。 Each of the following n lines contains a pair of distinct space-separated integers.以下 n 行中的每一行都包含一对不同的以空格分隔的整数。 I want to store the inputs of first column in one array and the second column in another array.我想将第一列的输入存储在一个数组中,将第二列的输入存储在另一个数组中。 I came up with this code, can you tell me where I went wrong and how to do it?我想出了这个代码,你能告诉我哪里出错了,怎么做吗?
n = int(input())
h = []
g = []
num = 0
for i in range(n):
m = map(int,input().split("\n"))
h.append(m)
for j in range(n):
ni = map(int,input().split("\n"))
h.append(ni)
When you read using input
you get the entire current line of input as a string, so each of your calls to input
after the first will return '1 2'
, '2 4'
and finally '3 4'
.当您使用input
读取时,您将整个当前输入行作为一个字符串,因此您在第一个之后对input
每个调用都将返回'1 2'
、 '2 4'
,最后是'3 4'
。
You need to split those strings on您需要将这些字符串拆分
(space) and then convert the values to integers and append them to the h
and g
lists. (空格),然后将值转换为整数并将它们附加到h
和g
列表中。 For example:例如:
for i in range(n):
this_h, this_g = map(int, input().split(' '))
h.append(this_h)
g.append(this_g)
print(h)
print(g)
Output:输出:
[1, 2, 3]
[2, 4, 4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.