You can download this code by clicking the button below.
This code is now available for download.
The function combines multiple iterable objects into a list of tuples, and fills in fillvalue if some iterables are shorter.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
"""
按照最长的可迭代对象长度返回一个元组列表,如果某个可迭代对象长度不足,则用fillvalue填充。
"""
# 获取最长的可迭代对象长度
longest = max((len(seq) for seq in args), default=0)
# 生成元组列表
return [tuple(seq[i] if i < len(seq) else fillvalue for seq in args) for i in range(longest)]