Generating Tuple Combinations from Iterables

  • Share this:

Code introduction


This function accepts an arbitrary number of iterable objects as arguments and returns a generator that yields tuples made up of the current elements from these iterables.


Technology Stack : zip

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip(*args):
    """
    创建一个元组,其中的元素是可迭代对象的元素组合。
    """
    def gen():
        iterator = iter(args)
        try:
            while True:
                yield tuple(next(iterator) for _ in args)
        except StopIteration:
            pass
    return gen()                
              
Tags: