Merging Iterables with Fillvalue

  • Share this:

Code introduction


This function merges multiple iterable objects into a single iterator. If the iterables are of unequal length, fillvalue is used to fill in the missing parts.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


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

    Args:
        *iterables: 可变数量的可迭代对象。
        fillvalue: 填充值,用于填充较短的迭代器。

    Returns:
        zip_longest对象,可以迭代以获取合并后的元素。

    """
    from itertools import zip_longest as _zip_longest

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