【问题标题】:Iterate through each integer python separated by a blankspace/endline遍历由空格/结束行分隔的每个整数 python
【发布时间】:2015-09-26 14:31:57
【问题描述】:

我是一名 Python 2.7 用户,最近切换到 python3。在读取由空格/结束线分隔的整数时,我使用了nex = iter(map(int,stdin.read().split())).next,其中nex() 充当输入整数的函数(假设在 x -> x=nex() 中输入整数值。但在 python3 中,这似乎不是工作。有人请提出一个在 Python3 中使用相同的解决方法。

【问题讨论】:

标签: python-3.x input


【解决方案1】:

.next() method is called .__next__() in Python 3。您可以使用next() function 编写单源 Python 2/3 兼容代码:

from functools import partial

nex = partial(next, iter(iterable))
print(nex())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2023-03-10
    相关资源
    最近更新 更多