Combining Iterables with Fillvalue

  • Share this:

Code introduction


The function combines multiple iterable objects, and if the iterable objects are of different lengths, it fills with a specified fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    """像zip函数一样组合多个可迭代对象,如果其中一个可迭代对象较短,则用fillvalue填充。
    
    Args:
        *args: 可变数量的可迭代对象。
        fillvalue: 缺失值填充。

    Returns:
        一个迭代器,返回元组,每个元组包含来自每个可迭代对象的元素。
    """
    from itertools import zip_longest
    return zip_longest(*args, fillvalue=fillvalue)                
              
Tags: