You can use input:
# the first line contains integer
integer1 = int(input())
# the second line contains the space separated list of integers
int_lst = list(map(int, input().split()))
# third line contains another integer.
integer2 = int(input())
list of lists for 10 lines:
lst = [list(map(int, input().split())) for _ in range(10)]
Answer from mozway on Stack OverflowYou can use input:
# the first line contains integer
integer1 = int(input())
# the second line contains the space separated list of integers
int_lst = list(map(int, input().split()))
# third line contains another integer.
integer2 = int(input())
list of lists for 10 lines:
lst = [list(map(int, input().split())) for _ in range(10)]
We can use the input() to receive the input from STDIN and cast it to int as the input() function returns the STDIN as string.
To receive an integer:
>>> x = int(input().strip())
12
>>> x
12
To convert the list of space separated integers to list:
>>> y = list(map(int,input().strip().split(' ')))
1 2 3 4 556
>>> y
[1, 2, 3, 4, 556]
To create 2D list of integers:
>>> rows = 3
>>> array = []
>>> for i in range(rows):
... each_line = list(map(int,input().strip().split(' ')))
... array.append(each_line)
...
1 2 3
4 5 6
7 8 9
>>> array
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
python - StdIn for Reading Inputs Hacker Rank Challenges - Stack Overflow
python - Python3: Using input() with stdin, like in hackerrank - Stack Overflow
Day 1: Data Types Discussions | Tutorials | HackerRank
Hackerrank how to read input and print output?
Videos
I am trying to do some challenges on Hackerrank and it says read input from STDIN and print output to STDOUT. What exactly does this mean and how do I read the input and print the output? I vaguely remember STDIN and STDOUT being used in C++ or something, but I don't know how to use them in Python.
The reason that you are getting the error is that readlines is consuming all your input on the first call. That means that the second time you call it, there's nothing there.
To make a 2D list of all the inputs, you have to work with the first call:
data = [line.rstrip().split() for line in sys.stdin.readlines()]
You may as well convert to integers up front:
data = [[int(x) for x in row] for row in data]
Now
arr = data[0]
A = set(data[2])
B = set(data[3])
If you need to input 3 lists(you know there will be 3 lists), then you can try the following:
myList1 = []
myList2 = []
myList3 = []
inStr1 = input()
inStr2 = input()
inStr3 = input()
for i in inStr1.split(" "):
myList1.append(int(i))
for i in inStr2.split(" "):
myList2.append(int(i))
for i in inStr3.split(" "):
myList3.append(int(i))
Here is a sample output I got:
