Merging Iterables with FillValue

  • Share this:

Code introduction


This function merges multiple iterable objects into a single iterable, if one of the iterable objects is shorter than the others, it is filled with fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将多个可迭代对象合并为一个新的可迭代对象,如果其中一个可迭代对象较其他短,则使用fillvalue填充。

    Args:
        *args: 任意多个可迭代对象。
        fillvalue: 填充值,当最长的可迭代对象结束时,其他较短的将使用此值填充。

    Returns:
        一个迭代器,包含所有输入可迭代对象的元素,如果某个可迭代对象已耗尽,则使用fillvalue填充。
    """
    from itertools import zip_longest as _zip_longest

    return _zip_longest(*args, fillvalue=fillvalue)                
              
Tags: