2

So, there's this example data set I wish to read in:

12
68 69 54 64 68 64 70 67
78 62 98 87

The first line is a number, n, for n numbers that will follow. I then want to read n numbers from the standard input into a single array: as shown in the example, it can spread across lines. Unlike C++, where the following can work:

for (int i=1;i<=n;i++) scanf("%d",&a[i]);

I have no idea if such an equivalent exists in Python. Any insight is greatly appreciated.

zhuoli xie
  • 21
  • 2
  • 2
    Welcome to Stack Overflow! What have you tried? What doesn't work? (Hint: there are Python tutorials on line, even for free. And references for syntax. You can't learn the language from questions on Stack Overflow. – Basya Jun 10 '20 at 19:19
  • I'd read in a line at a time and then break it down into the separate numbers (I'd probably do that in C also, but then, I never liked scanf). – Basya Jun 10 '20 at 19:23
  • Almost a duplicate of https://stackoverflow.com/questions/2285284/python-basics-how-to-read-n-ints-until-n-is-found-in-stdin – Basya Jun 10 '20 at 19:23
  • But, the amount of lines are not provided in advance, and the amount of numbers are given in advance, always in a separate line, so it seems to me that there's a difference. – zhuoli xie Jun 10 '20 at 19:25
  • This answer shows how to read until the end of the input: https://stackoverflow.com/questions/21235855/how-to-read-user-input-until-eof – Basya Jun 10 '20 at 19:25
  • There is a difference, yes. You can still read lines and split them until you get the correct number. It is less direct though. – Basya Jun 10 '20 at 19:26
  • The number of numbers is always on a line by itself, correct? – Basya Jun 10 '20 at 19:28
  • are they all two digit numbers? – Basya Jun 10 '20 at 19:30
  • Yes, the number of numbers is always the first number, occupying the entirety of the first line. – zhuoli xie Jun 10 '20 at 19:31
  • Is that the end of the file? Or do you have more of these [counter] + [item * counter] later? If it's the end of the file, why not just read everything? – Roy2012 Jun 10 '20 at 19:37
  • The numbers are only guaranteed to not exceed 2^31-1, and are not definite in length. I also wish the method to be applicable on a modern Online Judge system---- that means, not through file reads. – zhuoli xie Jun 10 '20 at 19:37
  • If the answers help you, you can say 'thank you' by upvoting them, and selecting one as an 'accepted solution' if it solved your problem (click on checkmark next to the question) – Basya Jun 11 '20 at 08:03
  • If the answers do not help you, then please edit the question to explain what more you need. – Basya Jun 11 '20 at 08:22

2 Answers2

1

I put this together from the references I gave you:

num_to_read = int(input())
num_read = 0
full_list = []

while num_read < num_to_read:
    lst = list(map(int, input().split()))
    num_read = num_read + len(lst)
    full_list.extend(lst)

# protect from too much input
del full_list[num_to_read:] 

print (num_to_read)
print (num_read)
print (full_list) 

Thank you to Roberto Caboni for catching a corner case which wasn't handled. Note the line protecting from too much input.

Basya
  • 1,477
  • 1
  • 12
  • 22
1

My solution is really similar to the one already posted. I just avoid counting the inserted elements, and perform a further check in the end cutting out any element exceeding the number specified in the first line:

num = int(input())

tempList = list()
while len(tempList) < num:
    tempList += list(map(int, input().split()))

print(tempList[:num])

So basically:

  1. I first read num
  2. I iterate getting inputs until the length of the list reaches (or goes beyond) num
  3. Line by line, I generate a sublist with the method explained here and append it to the list
  4. Before printing it I calculate the list slice so that exactly num elements are shown

Note: its limitation is that, by discarding the exceeding elements, it also consumes them. So these elements will not be available to any subsequent read.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39