473,395 Members | 1,471 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,395 software developers and data experts.

a question about zip...

I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...

Mar 8 '06 #1
5 1175
KraftDiner <bo*******@yahoo.com> wrote:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...

odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)
all [(0, 1), (2, 3), (4, 5), (6, 7)] tuple(all)

((0, 1), (2, 3), (4, 5), (6, 7))

Cheers,
--Jan Niklas
Mar 8 '06 #2
KraftDiner wrote:
I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...

tuple(zip(even, odd))

and if you fear for memory efficiency

tuple(iterator.izip(even, odd))
Mar 8 '06 #3
KraftDiner schrieb:
I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...


Use tuple(zip(...))

There is a reason that zip returns a list: appending to a list is considered a "natural" operation, whereas extending an
immutable(!) tuple creates a _new_ tuple for each entry. Which is a comparable costly operation - actually quadratic
complexity instead of linear.

So - if you need a tuple afterwards, convert it as shown above.

Diez
Mar 8 '06 #4
KraftDiner wrote:
I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...


Others have already told you how to do this. But I have
a question: why do you care that it is a tuple, not a
list? Is there something you need to do with it that
can be done with tuples but not lists?

In other words, while it isn't *wrong* to convert the
list to a tuple, perhaps it is *unnecessary*?
--
Steven.

Mar 9 '06 #5
KraftDiner wrote:
I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...

So apply tuple() to the list!

all = tuple(zip(even, odd))

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 9 '06 #6

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

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.