You can download this code by clicking the button below.
This code is now available for download.
This function is used to merge multiple iterable objects. If the length of the iterators is not consistent, fillvalue is used as the filling value.
Technology Stack : collections, itertools
Code Type : Function
Code Difficulty : Advanced
def zip_longest(*iterables, fillvalue=None):
# 从内置库collections中选取的zip_longest函数用于合并多个可迭代对象,并填充缺失值
from collections import deque
from itertools import zip_longest as zip_longest_itertools
def iter_from_queue(queue):
for item in queue:
if isinstance(item, deque):
for subitem in iter_from_queue(item):
yield subitem
else:
yield item
queue = [deque(iterable) for iterable in iterables]
for _ in range(max(len(q) for q in queue)):
for q in queue:
if q:
yield q.popleft()
while queue:
for q in queue:
if q:
yield q.popleft()
break
return zip_longest_itertools(*queue, fillvalue=fillvalue)