zip list with different length

Discussion in 'Python' started by s99999999s2003@yahoo.com, Apr 4, 2007.

  1. Guest

    hi
    suppose i have 2 lists, a, b then have different number of elements,
    say len(a) = 5, len(b) = 3
    >>> a = range(5)
    >>> b = range(3)
    >>> zip(b,a)

    [(0, 0), (1, 1), (2, 2)]
    >>> zip(a,b)

    [(0, 0), (1, 1), (2, 2)]

    I want the results to be
    [(0, 0), (1, 1), (2, 2) , (3) , (4) ]
    can it be done?
    thanks
     
    , Apr 4, 2007
    #1
    1. Advertising

  2. ginstrom Guest

    On Apr 4, 4:53 pm, wrote:
    > elements, say len(a) = 5, len(b) = 3
    > >>> a = range(5)
    > >>> b = range(3)

    ....
    > I want the results to be
    > [(0, 0), (1, 1), (2, 2) , (3) , (4) ]
    > can it be done?


    A bit cumbersome, but at least shows it's possible:

    >>> def superZip( a, b ):

    common = min( len(a), len(b) )
    results = zip( a[:common], b[:common] )
    if len( a ) < len( b ):
    a = b
    return results + [ (x,) for x in a[common:] ]

    >>> superZip( range( 5 ), range( 3 ) )

    [(0, 0), (1, 1), (2, 2), (3,), (4,)]
    >>> superZip( range( 3 ), range( 5 ) )

    [(0, 0), (1, 1), (2, 2), (3,), (4,)]
    >>> superZip( range( 0 ), range( 5 ) )

    [(0,), (1,), (2,), (3,), (4,)]
    >>> superZip( range( 3 ), range( 3 ) )

    [(0, 0), (1, 1), (2, 2)]

    Regards,
    Ryan Ginstrom
     
    ginstrom, Apr 4, 2007
    #2
    1. Advertising

  3. MC Guest

    Hi!

    Brutal, not exact answer, but:

    a = range(5)
    b = range(3)
    print zip(a+[None]*(len(b)-len(a)),b+[None]*(len(a)-len(b)))





    --
    @-salutations

    Michel Claveau
     
    MC, Apr 4, 2007
    #3
  4. Peter Otten Guest

    wrote:

    > suppose i have 2 lists, a, b then have different number of elements,
    > say len(a) = 5, len(b) = 3
    >>>> a = range(5)
    >>>> b = range(3)
    >>>> zip(b,a)

    > [(0, 0), (1, 1), (2, 2)]
    >>>> zip(a,b)

    > [(0, 0), (1, 1), (2, 2)]
    >
    > I want the results to be
    > [(0, 0), (1, 1), (2, 2) , (3) , (4) ]
    > can it be done?
    > thanks


    from itertools import izip, chain, repeat, takewhile, starmap

    def zip_longest(*seqs):
    padded = [chain(izip(s), repeat(())) for s in seqs]
    return takewhile(bool, starmap(sum, izip(izip(*padded), repeat(()))))

    Just to bring itertools to your attention :)

    Peter
     
    Peter Otten, Apr 4, 2007
    #4
  5. writes:

    C> hi
    > suppose i have 2 lists, a, b then have different number of elements,
    > say len(a) = 5, len(b) = 3
    > >>> a = range(5)
    > >>> b = range(3)
    > >>> zip(b,a)

    > [(0, 0), (1, 1), (2, 2)]
    > >>> zip(a,b)

    > [(0, 0), (1, 1), (2, 2)]
    >
    > I want the results to be
    > [(0, 0), (1, 1), (2, 2) , (3) , (4) ]
    > can it be done?


    map(lambda *t: filter(lambda x: x is not None,t),a,b)

    'as
     
    Alexander Schmolck, Apr 4, 2007
    #5
  6. MC <> writes:

    > Hi!
    >
    > Brutal, not exact answer, but:
    >
    > a = range(5)
    > b = range(3)
    > print zip(a+[None]*(len(b)-len(a)),b+[None]*(len(a)-len(b)))


    You reinvented map(None,a,b).

    'as
     
    Alexander Schmolck, Apr 4, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    1
    Views:
    626
    Andrew Thompson
    Sep 16, 2004
  2. Replies:
    0
    Views:
    1,804
  3. HARI OM
    Replies:
    0
    Views:
    17,495
    HARI OM
    May 3, 2005
  4. Girish Sahani
    Replies:
    17
    Views:
    586
    Boris Borcic
    Jun 9, 2006
  5. Graham Wood
    Replies:
    3
    Views:
    612
    A. Sinan Unur
    Jan 11, 2004
Loading...

Share This Page