zip iterators asserting for equal length in python

前端 未结 5 2332
不思量自难忘°
不思量自难忘° 2020-12-15 18:19

I am looking for a nice way to zip several iterables raising an exception if the lengths of the iterables are not equal.

In the case where the iterables

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 18:52

    Use more_itertools.zip_equal (v8.3.0+):

    Code

    import more_itertools as mit
    

    Demo

    list(mit.zip_equal(range(3), "abc"))
    # [(0, 'a'), (1, 'b'), (2, 'c')]
    
    list(mit.zip_equal(range(3), "abcd"))
    # UnequalIterablesError
    

    more_itertools is a third party package installed via λ pip install more_itertools

提交回复
热议问题