something for itertools
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sat Sep 23 17:28:50 EDT 2006
George Sakkis a écrit :
> Daniel Nogradi wrote:
>
>>In a recent thread,
>>http://mail.python.org/pipermail/python-list/2006-September/361512.html,
>>a couple of very useful and enlightening itertools examples were given
>>and was wondering if my problem also can be solved in an elegant way
>>by itertools.
>>
>>I have a bunch of tuples with varying lengths and would like to have
>>all of them the length of the maximal and pad with None's. So
>>something like
>>
>>a = ( 1, 2, 3 )
>>b = ( 10, 20 )
>>c = ( 'x', 'y', 'z', 'e', 'f' )
>>
>>should turn into
>>
>>a = ( 1, 2, 3, None, None )
>>b = ( 10, 20, None, None, None )
>>c = ( 'x', 'y', 'z', 'e', 'f' )
>>
>>Of course with some len( ) calls and loops this can be solved but
>>something tells me there is a simple itertools-like solution.
>>
>>Any ideas?
>
>
> Not the most readable one-liner of all times, but here it goes:
>
> a,b,c = zip(*map(None,a,b,c))
>
Simple and beautiful - but effectively requires a comment to explain
what's going on !-)
More information about the Python-list
mailing list