So, I want to take input of the following –
JavaScript
x
5
1
3
2
1 2
3
2 4
4
3 4
5
The first line contains an integer n. Each of the following n lines contains a pair of distinct space-separated integers. 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?
JavaScript
1
11
11
1
n = int(input())
2
h = []
3
g = []
4
num = 0
5
for i in range(n):
6
m = map(int,input().split("n"))
7
h.append(m)
8
for j in range(n):
9
ni = map(int,input().split("n"))
10
h.append(ni)
11
Advertisement
Answer
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'
.
You need to split those strings on
(space) and then convert the values to integers and append them to the h
and g
lists. For example:
JavaScript
1
7
1
for i in range(n):
2
this_h, this_g = map(int, input().split(' '))
3
h.append(this_h)
4
g.append(this_g)
5
print(h)
6
print(g)
7
Output:
JavaScript
1
3
1
[1, 2, 3]
2
[2, 4, 4]
3