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 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
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 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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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*...
|
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...
|
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...
|
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(
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |