Custom Variable-Length List Combiner

  • Share this:

Code introduction


Custom function that combines a variable number of list arguments into a new list, filling the shorter lists with a specified fillvalue if necessary.


Technology Stack : itertools (built-in library)

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=None):
    # 将可变数量的参数列表组合成一个新的列表,如果某个列表较短,则用fillvalue填充
    # 使用内置的zip_longest函数,因为它是collections模块中的一个函数,符合要求

    from itertools import zip_longest

    def custom_zip_longest(*args, fillvalue=None):
        return list(zip_longest(*args, fillvalue=fillvalue))

    return custom_zip_longest

# JSON描述