python - 导入下一个()python 2.5

标签 python next

我使用的是 itertools 中成对配方的一个稍微修改过的版本,看起来像这样

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b) 

现在我需要用 python 2.5 运行代码,其中 next() 函数抛出以下异常:

<type 'exceptions.NameError'>: global name 'next' is not defined

有没有办法在 python 2.5 中使用 next()?或者我需要如何修改函数才能使其正常工作?

最佳答案

您可以自己轻松地提供此函数的定义:

_sentinel = object()
def next(it, default=_sentinel):
    try:
        return it.next()
    except StopIteration:
        if default is _sentinel:
            raise
        return default

关于python - 导入下一个()python 2.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652404/

相关文章:

python - tensorflow 类型错误: cannot unpack non-iterable float object

next.js - 我得到一个 NEXT.JS "Hydration failed because the initial UI does not match what was rendered on the server"

Dojo 根据当前事件目标获取下一个兄弟

python - 以我自己的观点使用 Django Admin 搜索引擎

Python - 将附加反斜杠添加到文件路径

python - Django 装饰器@transaction.non_atomic_requests 在 ViewSet 方法中不起作用

javascript - jQuery.next() 返回空对象

python - 如何将一个字符串与列表中的下一个字符串进行比较?

python - 使用搜索和调用 next() 读取文件时有没有办法返回?

python - 断言包含特定长度字符串的列表