473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

zip list with different length

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 '07 #1
5 10595
On Apr 4, 4:53 pm, s99999999s2...@yahoo.com 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

Apr 4 '07 #2
MC
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
Apr 4 '07 #3
s9************@yahoo.com 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
Apr 4 '07 #4
s9************@yahoo.com writes:

Chi
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

Apr 4 '07 #5
MC <XX*****@XX.XmclaveauX.comwrites:
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
Apr 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
6
by: Osiris | last post by:
Is the following intuitively feasible in Python: I have an array (I come from C) of identical objects, called sections. These sections have some feature, say a length, measured in mm, which is...
8
by: Rex | last post by:
Hi All, I need to create a 2-dimensional group of values, the first dimension having 8 rows, while the 2nd is variable. I was thinking of using List<T>, but MAYBE List<Tis not the way to go (I...
15
RMWChaos
by: RMWChaos | last post by:
In my ongoing effort to produce shorter, more efficient code, I have created a "chicken and egg" / "catch-22" problem. I can think of several ways to fix this, none of them elegant. I want my code...
33
by: John Salerno | last post by:
Is it possible to write a list comprehension for this so as to produce a list of two-item tuples? base_scores = range(8, 19) score_costs = print zip(base_scores, score_costs) I can't think...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.