Loop in a loop?

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Jan 17 11:38:58 EST 2008


cokofreedom at gmail.com a écrit :
(snip)
> couldn't you just do something like
> 
> if len(array1) is not len(array2):

*never* use the identity operator to test equality ! The fact that 
CPython memoize small integers is an implementation detail, *not* a part 
of the language specification.

>     if len(array1) < len(array2):
>         max_length = len(array2) - len(array1)
>         array1.extend([None for i in xrange(0, max_length)])
>     elif len(array1) > len(array2):
>         max_length = len(array1) - len(array2)
>         array2.extend([None for i in xrange(0, max_length)])


Never heard of the builtin max() function ?-)

def pad(*lists, **kw):
    padding = kw.get('padding', None)
    lists_lens = [len(alist) for alist in lists]
    padlen = max(lists_lens)
    return [
             alist + ([padding] * (padlen - list_len))
             for list_len, alist in zip(lists_lens, lists)
           ]

for i in zip(*pad(range(3), range(5, 10))):
    print i


Now there are very certainly smart solutions using itertools, but the 
one I cooked is way too ugly so I'll leave this to itertools masters !-)



More information about the Python-list mailing list