Python input() 不读取整个输入数据
我正在尝试从 stdin 读取数据,实际上我正在使用 Ctrl+C、Ctrl+V 将值传递到 cmd,但它会在某个时刻停止该过程。总是同一个点。输入文件为.in类型,格式为第一行是一个数字,接下来的3行包含以空格分隔的一组数字。我正在使用Python 3.9.9。此外,较长的文件也会出现此问题(集合中的元素数量> 10000),而输入较短则一切正常。看来内存刚刚耗尽。我有以下方法:
def readData():
# Read input
for line in range(5):
x = list(map(int, input().rsplit()))
if(line == 0):
nodes_num = x[0]
if(line == 1):
masses_list = x
if(line == 2):
init_seq_list = x
if(line == 3):
fin_seq_list = x
return nodes_num, masses_list, init_seq_list, fin_seq_list
以及有效的数据:
6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1
以及长输入文件: https://pastebin.com/atAcygkk 它停在序列:... 2421 1139 322],所以它就像第四行的一部分。
I'm trying to read the data from stdin, actually I'm using Ctrl+C, Ctrl+V to pass the values into cmd, but it stops the process at some point. It's always the same point. The input file is .in type, formating is that the first row is one number and next 3 rows contains the set of numbers separated with space. I'm using Python 3.9.9. Also this problem occurs with longer files (number of elements in sets > 10000), with short input everything is fine. It seems like the memory just run out. I had following aproach:
def readData():
# Read input
for line in range(5):
x = list(map(int, input().rsplit()))
if(line == 0):
nodes_num = x[0]
if(line == 1):
masses_list = x
if(line == 2):
init_seq_list = x
if(line == 3):
fin_seq_list = x
return nodes_num, masses_list, init_seq_list, fin_seq_list
and the data which works:
6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1
and the long input file:
https://pastebin.com/atAcygkk
it stops at the sequence: ... 2421 1139 322], so it's like a part of 4th row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从“标准输入”读取输入,您只需使用
stdin
流。由于您的数据全部位于行上,因此您只需读取 EOL 分隔符即可,而不必自己使用某些索引号来跟踪行。此代码将在作为 python3.9 sowholeinput.py运行时工作。 atAcygkk.txt
,或cat atAcygkk.txt| python3.9 sowholeinput.py
。有趣的是,正如您所描述的,当使用终端剪切和粘贴来粘贴文本时,它不起作用。这意味着该方法的限制,而不是 Python 本身的限制。
To read input from "standard input", you just need to use the
stdin
stream. Since your data is all on lines you can just read until the EOL delimiter, not having to track lines yourself with some index number. This code will work when run aspython3.9 sowholeinput.py < atAcygkk.txt
, orcat atAcygkk.txt| python3.9 sowholeinput.py
.Interestingly, it does not work, as you describe, when pasting the text using the terminal cut-and-paste. This implies a limitation with that method, not Python itself.