Combining Iterables with Fillvalue

  • Share this:

Code introduction


Combines multiple iterable objects into a list of tuples, if an iterable object is shorter, it uses the specified fillvalue to fill.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将多个可迭代对象组合成元组列表,如果其中某个可迭代对象长度不足,则使用fillvalue填充。

    Args:
        *args: 可变数量的可迭代对象。
        fillvalue: 当某个可迭代对象长度不足时,用来填充的值。

    Returns:
        列表,包含元组,每个元组的长度等于最长的可迭代对象。
    """
    from itertools import zip_longest
    return list(zip_longest(*args, fillvalue=fillvalue))                
              
Tags: