小编Big*_*igZ的帖子

我可以在python 2.4中使用什么而不是next()

我需要模拟izip_longestitertools在Python 2.4

import itertools
class Tools:
    @staticmethod
    def izip_longest(*args, **kwds):
        # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
        fillvalue = kwds.get('fillvalue')
        counter = [len(args) - 1]
        def sentinel():
            if not counter[0]:
                raise ZipExhausted
            counter[0] -= 1
            yield fillvalue
        fillers = itertools.repeat(fillvalue)
        iterators = [itertools.chain(it, sentinel(), fillers) for it in args]
        try:
        while iterators:
            yield tuple(map(next, iterators))
        except ZipExhausted:
            pass       


class ZipExhausted(Exception):
    pass
Run Code Online (Sandbox Code Playgroud)

一切正常,直到我到达yield tuple(map(next, iterators)); Python 2.4抛出一个

NameError: global name 'next' is not …
Run Code Online (Sandbox Code Playgroud)

python python-2.4 next python-itertools

3
推荐指数
1
解决办法
774
查看次数

标签 统计

next ×1

python ×1

python-2.4 ×1

python-itertools ×1