Hemal
Hemal

Reputation: 67

Read unknown number of ints separated by spaces or ends of lines

I am trying to find a way to get this right, I found some bits that answer this question only partially, such as:

from sys import stdin 
lines = stdin.read().splitlines()

but this would only take in ints separated by lines

inp = list(map(int,input().split()))

while this only reads ints separated by spaces

I am stuck on this and couldn't figure out an intersection for the two. I am trying to learn EOF function.

Upvotes: 0

Views: 19

Answers (1)

RafalS
RafalS

Reputation: 6324

import sys

numbers = []
for line in sys.stdin:
    numbers += [int(number) for number in line.split()]
print(numbers)

Keep in mind that you have to explicitly send EOF on the terminal for the loop to finish (ctrl+d in bash). Otherwise, it will be stuck in for loop forever.

Upvotes: 1

Related Questions