Combining Iterables with itertools.zip_longest

  • Share this:

Code introduction


This function uses the itertools.zip_longest function to combine multiple iterable objects whose lengths may differ. If data in a certain iterable is exhausted, fillvalue is used to fill the remaining positions.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*iterables, fillvalue=None):
    zip_longest(*iterables, fillvalue=None)
    """
    使用 itertools 库中的 zip_longest 函数来合并多个可迭代对象,如果某些可迭代对象长度不同,则用 fillvalue 填充。
    """
    import itertools

    # 使用 itertools.zip_longest 来合并多个可迭代对象
    for combination in itertools.zip_longest(*iterables, fillvalue=fillvalue):
        print(combination)                
              
Tags: