-2

Sample input 1:

1 10
100 200
201 210
900 1000

Sample input 2:

4 49
2 59

So I don't know how many lines the input would be, and I want to organize the input into a 2d array. The lines are submitted all at once (no multiple input()).

Edit: Sorry I'm terrible with I/O. Here's what I want:

nums = []
for i in range(0,endofinput):
    nums.append(list(map(int, input().split())))
print(nums)

Problem is, I don't know how many inputs there will be. I don't have control over the input, so I can't just make a while loop to detect when the next input is "". Is there possibly a way to know how many lines of input I will be getting?

agiledatabase
  • 29
  • 1
  • 4
McGovno
  • 25
  • 1
  • 5
  • And what have you tried? –  Jan 04 '17 at 17:15
  • I've tried just splitting the string as such: nums = list(map(int, input().split())) Clearly doesn't work. I also tried to write a for loop for when input() == "", but then I realized there is only one input which is the entire block. Really no idea on how to approach this. – McGovno Jan 04 '17 at 17:33
  • 1
    What do you mean, "submitted all at once"? `input` reads a single string from standard input, that string being terminated by whatever character(s) currently understood to terminate a single line. – chepner Jan 04 '17 at 17:39
  • Hm, best to edit your question and describe the problem specifically (what you're trying to do exactly with a reproducible example). I thought your issue was elsewhere. Press the edit link and add this to your question. – Dimitris Fasarakis Hilliard Jan 04 '17 at 17:43
  • 1
    Possible duplicate of [Raw input across multiple lines in Python](http://stackoverflow.com/questions/11664443/raw-input-across-multiple-lines-in-python) – Aman Jaiswal Jan 04 '17 at 18:19

2 Answers2

1

How much control do you have over the format of the input? (Is it coming from some external automated system? From a User? From another script?)

Assuming you have some measure of control, you can implement a sentinel value, such as an empty line, to detect the end of the input. Then

l = []
data = input(':')
while data:
    l.append(list(map(int, data.split())))

will read lines of whitespace-separated integers into a list of lists of integers until it reads an empty line.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

Usually, input() accepts single lines only. The only way I know to pass multiple lines is to paste a copied block of text. The program processes the lines as if they had been entered one by one. The only way to tell your program that the end of the input has been reached is to enter either some stop-code or, more stylish, to press Ctrl-D, the classic End-Of-File marker.

print("Enter multiple lines, press Ctrl-D after last line:")
Array=[]
while True:
    try:
        Array.append([int(a) for a in input().split(" ")])
    except EOFError:
        print(len(Array),"lines of data have been received.")
        break

Result after pasting four lines and pressing [Enter] and [Ctrl-D]:

Enter multiple lines, press Ctrl-D after last line:
1 10
100 200
201 210
900 1000
4 lines of data have been received.
>>> Array
[[1, 10], [100, 200], [201, 210], [900, 1000]]
>>>         
Marvo
  • 523
  • 7
  • 16