简体   繁体   English

如何将多行输入存储到列表中(python)

[英]How to store multi-line input into a list (python)

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?如果没有任何特定指标,您如何识别最终用户输入?

Keep calling the input() function until the line it reads in is empty.继续调用input()函数,直到它读入的行为空。 The use the .split method (no args = space as deliminator).使用.split方法(没有 args = space 作为分隔符)。 Use a list-comp to convert each string to an int and append this to your examList .使用list-comp将每个string转换为int并将其附加到您的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:根据您的输入, examList是:

[[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 != '' ).上述方法适用于Python3 ,它允许您调用input()并且不输入任何内容(这就是为什么我们使用它作为信号来检查我们是否已完成 - i != '' )。

However, from the docs , we see that, in Python2 , an empty entry to input() throws an EOF error.但是,从docs 中,我们看到,在Python2input()的空条目会引发EOF错误。 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.为了解决这个问题,我想我们可以让它在输入诸如END的字符串时结束多行输入。 This means that you must stop the entry with a line saying 'END' :这意味着您必须用一行说'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.请注意,我使用raw_input作为不执行类型转换。

which works the same as above.其工作原理与上述相同。

You can use sys.stdin.readlines() .您可以使用sys.stdin.readlines()

For example, start with例如,从

import sys
user_input = sys.stdin.readlines()

at this point, the value of user_input should be此时user_input的值应该是

['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 .我们在这里做的是迭代user_input For each input_string , we split() the words, convert them to int and put them back into a new list.对于每个input_string ,我们split()单词,将它们转换为int并将它们放回新列表中。 Then, the new list will be added into examList .然后,新列表将被添加到examList

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']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM