我需要在Python从itertools
效仿izip_longest
2.4我可以使用,而不是下一个()在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
一切工作正常,直到我到达yield tuple(map(next, iterators))
; Python 2.4抛出一个
NameError: global name 'next' is not defined
错误和退出。
我可以用什么来代替next
来使izip_longest
在Python 2.4中运行?
或者Python 2.4中是否有其他函数返回与izip_longest()
相同的结果?
出于好奇,你为什么要使用一个类具有静态方法?为什么不只是做一个功能呢? – 2014-10-06 13:59:57
有点相关(不完全重复):http://stackoverflow.com/q/25810855/1639625 – 2014-10-06 14:00:23