Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表中输入下一行分隔的数字_Python_Python 2.7_Python 3.x - Fatal编程技术网

Python 在列表中输入下一行分隔的数字

Python 在列表中输入下一行分隔的数字,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我从标准输入流中获取输入,如下所示: 1 2 3 4 5 6 我想在一个列表中以整行分隔输入,并将其打印为 ['1','2','3','4','5','6']. 对于字符串: My_String = "" a = input() while a != "": My_String += a a = input() return My_String 名单 My_List = [] a = input() while a != "": My_List.append(

我从标准输入流中获取输入,如下所示:

1
2
3
4
5
6
我想在一个列表中以整行分隔输入,并将其打印为

 ['1','2','3','4','5','6'].
对于字符串:

My_String = ""
a = input()
while a  != "":
    My_String += a
    a = input()
return My_String
名单

My_List = []
a = input()
while a  != "":
    My_List.append(a)
    a = input()
return My_List
对多行输入使用
sys.stdin.read()

import sys
data = sys.stdin.read()
data_list = data.strip().split('\n')

我做了如下同样的事情:

import sys
arr = sys.stdin.read()
list = data.splitlines()

如果需要固定数量的输入,请在for循环中执行

lst=[]
for i in range(6):
    lst.append(input())

print lst
要一直启动进程直到
键盘中断
,您可以执行以下操作:

lst=[]
while True:
    lst.append(input())
    print lst 
如果您想在
ctl
+
c

import signal
import sys
def signal_handler(signal, frame):
    print lst
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

lst=[]
while(True):
    lst.append(input())
如果要输入到特定字符,例如
空格
输入:

lst=[]
while True:
    demo = raw_input()
    if demo==" ":
        break
    lst.append(demo)

print lst

所以你想调用
input()
六次?我想在列表中或作为字符串表示什么?与其调用
input()
六次,我们还有其他方法可以这样做吗?请参阅内置方法。如果您为个人使用变量,列表需要什么?@AhsanulHaque为个人使用变量?你是什么意思?单个输入的变量?首先定义一堆变量,然后将其打印成一个列表,这不是很有趣吗。另外,这不是一个广义的解决方案。@AhsanulHaque-Hum……好吧,我想你是对的。但我不知道如何优化它。有什么想法吗?看我的答案,虽然这对固定输入有效,但op不希望这样。不,输入数的长度是unknown@AthulRaj,输入何时结束?键盘中断?