Packing Variable-Length Iterables into Tuples

  • Share this:

Code introduction


This function packs multiple variable-length iterable objects into a list of tuples. If the input sequences are of unequal length, the shorter ones are filled with the specified fillvalue.


Technology Stack : itertools

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_longest(*args, fillvalue=()):
    """将可变数量的长输入序列打包成元组列表,较短的序列会用fillvalue填充。

    Args:
        *args: 一个或多个可迭代对象。
        fillvalue: 用于填充较短序列的值。

    Returns:
        列表,其中包含元组,每个元组包含来自输入序列的元素,如果序列长度不等,则较短的序列用fillvalue填充。
    """
    from itertools import zip_longest as zip_longest_imp
    return list(zip_longest_imp(*args, fillvalue=fillvalue))                
              
Tags: