Combining Iterables with Fillvalue

  • Share this:

Code introduction


Combines multiple iterable objects into tuples. If an iterable is shorter than the others, fillvalue is used to fill it.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将可迭代对象组合成元组,如果某个可迭代对象较短,则使用fillvalue填充。

    Args:
        *args: 可变数量的可迭代对象。
        fillvalue: 如果某个可迭代对象较短,则用此值填充。

    Returns:
        zip对象,其中较短的序列用fillvalue填充。

    Example:
        >>> list(zip_longest([1, 2, 3], ['a', 'b'], fillvalue=0))
        [(1, 'a', 0), (2, 'b', 0), (3, None, 0)]
    """
    from itertools import zip_longest as _zip_longest

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