You can download this code by clicking the button below.
This code is now available for download.
This function combines multiple lists or tuples into one list. If a list is shorter, it is filled with fillvalue.
Technology Stack : zip_longest, next, iter, fillvalue
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*args, fillvalue=None):
# 将可变数量的列表或元组归并为一个列表,如果其中某个可变数量较短,则使用fillvalue填充
def _zip_longest(*iterables, fillvalue):
iterators = [iter(iterable) for iterable in iterables]
while True:
result = []
for iterator in iterators:
try:
result.append(next(iterator))
except StopIteration:
result.append(fillvalue)
if not any(result):
break
yield result
return _zip_longest(*args, fillvalue=fillvalue)