如果迭代的长度不相等,我正在寻找一种很好的方法来压缩几个迭代,引发异常.
在迭代是列表或具有len方法的情况下,该解决方案是干净和容易的:
def zip_equal(it1,it2): if len(it1) != len(it2): raise ValueError("Lengths of iterables are different") return zip(it1,it2)
但是,如果it1和it2是生成器,则前一个函数失败,因为长度未定义TypeError:类型为“generator”的对象没有len().
我想象,itertools
模块提供了一种简单的方法来实现,但到目前为止我还没有找到它.我想出了这个自制的解决方案:
def zip_equal(it1,it2): exhausted = False while True: try: el1 = next(it1) if exhausted: # in a prevIoUs iteration it2 was exhausted but it1 still has elements raise ValueError("it1 and it2 have different lengths") except StopIteration: exhausted = True # it2 must be exhausted too. try: el2 = next(it2) # here it2 is not exhausted. if exhausted: # it1 was exhausted => raise raise ValueError("it1 and it2 have different lengths") except StopIteration: # here it2 is exhausted if not exhausted: # but it1 was not exhausted => raise raise ValueError("it1 and it2 have different lengths") exhausted = True if not exhausted: yield (el1,el2) else: return
it1 = (x for x in ['a','b','c']) # it1 has length 3 it2 = (x for x in [0,1,2,3]) # it2 has length 4 list(zip_equal(it1,it2)) # len(it1) < len(it2) => raise it1 = (x for x in ['a',3]) # it2 has length 4 list(zip_equal(it2,it1)) # len(it2) > len(it1) => raise it1 = (x for x in ['a','c','d']) # it1 has length 4 it2 = (x for x in [0,3]) # it2 has length 4 list(zip_equal(it1,it2)) # like zip (or izip in python2)
我是否俯视任何替代解决方案?我的zip_equal函数有更简单的实现吗?
PS:我在Python 3中写了这个问题,但Python 2解决方案也是受欢迎的.
解决方法
我可以使用itertools.zip_longest()并使用itertools.zip_longest()引发异常,如果用于填充较短的迭代的sentinel值存在于生成的元组中:
from itertools import zip_longest def zip_equal(*iterables): sentinel = object() for combo in zip_longest(*iterables,fillvalue=sentinel): if sentinel in combo: raise ValueError('Iterables have different lengths') yield combo
不幸的是,我们不能使用带有yield的zip()来避免Python代码循环,每次迭代都要进行测试;一旦最短的迭代器用尽,zip()将推进所有以前的迭代器,从而吞噬证据,如果那些中只有一个额外的项目.