0

How would you store a multi-line input into an list?

For example:

3
2 1
1 1 0
2 1 1
4 3 0 1 2
2 
1 2
1 3

How would I take that input and store it as a list like so:

examList = [
      [3],
      [2,1],
      [1,1,0], 
      [2,1,1], 
      [4,3,0,1,2], 
      [2],
      [1,2],
      [1,3]
]

How do you identify the end user input if there any no specific indicators?

3 Answers 3

2

Keep calling the input() function until the line it reads in is empty. The use the .split method (no args = space as deliminator). Use a list-comp to convert each string to an int and append this to your examList.

Here's the code:

examList = []
i = input()
while i != '':
    examList.append([int(s) for s in i.split()])
    i = input()

And with your input, examList is:

[[3], [2, 1], [1, 1, 0], [2, 1, 1], [4, 3, 0, 1, 2], [2], [1, 2], [1, 3]]

The above method is for Python3 which allows you to call input() and enter nothing (which is why we use this as a signal to check that we are done - i != '').

However, from the docs, we see that, in Python2, an empty entry to input() throws an EOF error. To get around this, I guess we could just make it so that the multi-line input is ended when the string such as: END is entered. This means that you must stop the entry with a line saying 'END':

examList = []
i = raw_input()
while i != 'END':
    examList.append([int(s) for s in i.split()])
    i = raw_input()

Note that I've used raw_input as to not perform type conversion.

which works the same as above.

Sign up to request clarification or add additional context in comments.

6 Comments

@HuStmpHrrr Python2 is so outdated, I doubt they are using it, and a quick look over the OPs other questions shows that in fact they do use Python3
@Joelddon your code resulted in "EOFError: EOF when reading a line" when ran. How would you determine the end of the input?
@WilliamMerritt I will update the answer, you must be on Python2, am I right?
@Joelddon I'm running Python3.
@Joelddon, that code still has issues when getting the last element of the input ('1 3' in this case). I don't understand how while i != '' iterates to the end of the input successfully. Still getting EOF errors.
|
2

You can use sys.stdin.readlines().

For example, start with

import sys
user_input = sys.stdin.readlines()

at this point, the value of user_input should be

['3\n', '2 1\n', '1 1 0\n', '2 1 1\n', '4 3 0 1 2\n', '2\n', '1 2\n', '1 3']

then, you should be able to get your desired output by performing the following processing

examList = []

for input_string in user_input:
   examList.append([int(value) for value in input_string.split()])

What we are doing here is iterating through user_input. For each input_string, we split() the words, convert them to int and put them back into a new list. Then, the new list will be added into examList.

Comments

1

Here's an effective one-liner:

>>> inp = '''3
2 1
1 1 0
2 1 1
4 3 0 1 2
2 
1 2
1 3'''
>>> [i.split() for i in inp.split('\n')]

[['3'], ['2', '1'], ['1', '1', '0'], ['2', '1', '1'], ['4', '3', '0', '1', '2'], ['2'], ['1', '2'], ['1', '3']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.