zip_longest这是有问题的,因为任何解决方案都会
fillvalue在输入中无提示地删除它(可以解决此问题,但总会有一点麻烦)。
最普遍的解决方案是
roundrobin从配方的
itertools模块:
from itertools import cycle, islicedef roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis num_active = len(iterables) nexts = cycle(iter(it).__next__ for it in iterables) while num_active: try: for next in nexts: yield next() except StopIteration: # Remove the iterator we just exhausted from the cycle. num_active -= 1 nexts = cycle(islice(nexts, num_active))对于您的输入,您将执行以下操作:
mylist = [ [4,7,9,10], [5,14,55,24,121,56, 89,456, 678], [100, 23, 443, 34, 1243,] ....]print(list(roundrobin(*mylist)))



