Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > zip list with different length

Reply
Thread Tools

zip list with different length

 
 
s99999999s2003@yahoo.com
Guest
Posts: n/a
 
      04-04-2007
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

 
Reply With Quote
 
 
 
 
ginstrom
Guest
Posts: n/a
 
      04-04-2007
On Apr 4, 4:53 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
MC
Guest
Posts: n/a
 
      04-04-2007
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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-04-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Alexander Schmolck
Guest
Posts: n/a
 
      04-04-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Alexander Schmolck
Guest
Posts: n/a
 
      04-04-2007
MC <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possibility to add a zip-file to a new zip-file with "add to zip" (right-click) ?? erikkie@casema.nl Computer Support 4 06-26-2006 12:18 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
java.util.zip.ZipException: error in opening zip file hari_om@hotmail.com Java 0 05-03-2005 04:47 PM
java.util.zip - problem opening some legitimite me zip files Alex Hunsley Java 1 09-16-2004 02:06 PM
Can I list contents of nested zipfile using Archive::Zip withoutextracting the outermost zip? Graham Wood Perl Misc 3 01-11-2004 01:17 AM



Advertisments