Python input() 不读取整个输入数据

发布于 2025-01-14 07:50:27 字数 808 浏览 1 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2025-01-21 07:50:27

要从“标准输入”读取输入,您只需使用 stdin 流。由于您的数据全部位于行上,因此您只需读取 EOL 分隔符即可,而不必自己使用某些索引号来跟踪行。此代码将在作为 python3.9 sowholeinput.py 运行时工作。 atAcygkk.txt,或cat atAcygkk.txt| python3.9 sowholeinput.py

def read_data():
    stream = sys.stdin
    num = int(stream.readline())
    masses = [int(t) for t in stream.readline().split()]
    init_seq = [int(t) for t in stream.readline().split()]
    fin_seq = [int(t) for t in stream.readline().split()]
    return num, masses, init_seq, fin_seq

有趣的是,正如您所描述的,当使用终端剪切和粘贴来粘贴文本时,它不起作用。这意味着该方法的限制,而不是 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 as python3.9 sowholeinput.py < atAcygkk.txt, or cat atAcygkk.txt| python3.9 sowholeinput.py.

def read_data():
    stream = sys.stdin
    num = int(stream.readline())
    masses = [int(t) for t in stream.readline().split()]
    init_seq = [int(t) for t in stream.readline().split()]
    fin_seq = [int(t) for t in stream.readline().split()]
    return num, masses, init_seq, fin_seq

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文