Combining Iterables with Fillvalue

  • Share this:

Code introduction


This function combines multiple iterable objects into a list of tuples. If some iterables are shorter, they are filled with the fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


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

    Args:
        *args: 任意数量的可迭代对象。
        fillvalue: 填充较短可迭代对象的值。

    Returns:
        一个列表,其中包含元组,元组中的元素来自输入的可迭代对象。
    """
    from itertools import zip_longest
    return list(zip_longest(*args, fillvalue=fillvalue))                
              
Tags: