wie kann man das folgende Problem elegant lösen? Ich versuche es mal in der von itertools bekannten Kurzschreibweise klar zu machen:
Code: Alles auswählen
func(p, q, ...) p0, q0, ... p1, q1, ... plast, qlast, ...
# z.B.
func('ABC', 'DEF') --> A D B E C F
Ich habe es mit Hilfe der flatten()-Funktion aus dem Brownie-Paket gelöst:
Code: Alles auswählen
In [8]: from itertools import izip_longest
In [9]: from brownie.itools import flatten
In [10]: a, b = range(5), range(5, 9)
In [11]: a, b
Out[11]: ([0, 1, 2, 3, 4], [5, 6, 7, 8])
In [12]: list(flatten(izip_longest(a, b)))
Out[12]: [0, 5, 1, 6, 2, 7, 3, 8, 4, None]
Code: Alles auswählen
def merge(*iterables, **kwargs):
fillvalue = kwargs.get('fillvalue')
for item in flatten(izip_longest(*iterables, fillvalue=fillvalue)):
yield item
