This is probably so easy that I'll be embarrassed by the answer. While
enhancing and refactoring some old code, I was just changing some map()s to
list comprehensions, but I couldn't see any easy way to change a zip() to a
list comprehension. Should I just let those sleeping dogs lie? (list
comprehensions seem more readable than map(), but if the list comprehension
that does the equivalent of zip() is less expressive than zip(), I'll stick
with zip()). 3 3336
Matt Gerrans wrote: This is probably so easy that I'll be embarrassed by the answer. While enhancing and refactoring some old code, I was just changing some map()s to list comprehensions, but I couldn't see any easy way to change a zip() to a list comprehension. Should I just let those sleeping dogs lie? (list comprehensions seem more readable than map(), but if the list comprehension that does the equivalent of zip() is less expressive than zip(), I'll stick with zip()).
Hmmm... I couldn't do any better than: seqs = [(1, 2, 3), (4, 5, 6, 7)] zip(*seqs)
[(1, 4), (2, 5), (3, 6)] [tuple(seq[i] for seq in seqs)
.... for i in range(min(len(s eq) for seq in seqs))]
[(1, 4), (2, 5), (3, 6)]
I think I'll stick with zip. ;)
I too have been trying to make my code more conformant with Python 3000
recommendations (e.g. removing maps in favor of LCs or GEs, replacing
lambdas with named functions, etc.) but I've left zip pretty much alone.
Steve
Matt Gerrans wrote: This is probably so easy that I'll be embarrassed by the answer.
While enhancing and refactoring some old code, I was just changing some
map()s to list comprehensions, but I couldn't see any easy way to change a
zip() to a list comprehension. Should I just let those sleeping dogs lie?
(list comprehensions seem more readable than map(), but if the list
comprehension that does the equivalent of zip() is less expressive than zip(), I'll
stick with zip()).
I don't recall seeing zip on the list of things that were considered
bad for Python 3K, probably because it's not functional programming (a
la map, reduce, and filter) but rather just list manipulation.
I can't think of a good way to replace zip with a list comp, and I
doubt there is such a way. Think about it: a list comp is semantically
equivalent to a certain for loop (or loops, perhaps with some if-blocks
in there as well). A list comp can do only what a for loop can do,
nothing more.
Well, zip was created to address a deficiency in for-looping, namely
that it was unwieldy to loop through two lists side-by-side. I suspect
that, if there were a simple way to accomplish what zip does in a list
comp, there would have also been an easy way to do it with for loops,
and therefore zip would not have had any reason to exist.
So, unless the BDFL waves his magic wand and adds some new syntax for
for loops (unlikely), I think you can operate under the assumption that
zip will be Python 3000 Certified (tm).
Having said that, you might want to consider using itertools.izip
instead. It works just like zip, but returns an iterator instead of a
list. Good for those length-ten-million lists you wanted to iterate
side-by-side.
--
CARL BANKS
> This is probably so easy that I'll be embarrassed by the answer. While enhancing and refactoring some old code, I was just changing some map()s to list comprehensions, but I couldn't see any easy way to change a zip() to a list comprehension. Should I just let those sleeping dogs lie? (list comprehensions seem more readable than map(), but if the list comprehension that does the equivalent of zip() is less expressive than zip(), I'll stick with zip()).
Leave in zip(), enumerate(), and reversed().
They are meant to be used with listcomps and genexps.
If you want to go the extra distance, itertools.izip( ) can offer a performance
boost and better memory utilization than zip(). It can be used almost anywhere
as long as the app doesn't demand a real list.
Raymond Hettinger 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* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=, , , , ]
I thought there has to be a *really* easy way of doing it - after a
|
by: Moosebumps |
last post by:
Does anyone here find the list comprehension syntax awkward?
I like it because it is an expression rather than a series of statements,
but it is a little harder to maintain it seems.
e.g. you could do:
result =
for element in list:
if element == 'blah':
|
by: Chris P. |
last post by:
Hi. I've made a program that logs onto a telnet server, enters a
command, and then creates a list of useful information out of the
information that is dumped to the screen as a result of the command.
Here's a generic version of the code in question:
#####
# Prior code opens telnet connection "tn" and logs in.
tn.read_until('> ')
tn.write('THE COMMAND IS HERE\n')
|
by: jena |
last post by:
hello,
when i create list of lambdas:
l=]
then l() returns 'C', i think, it should be 'A'
my workaround is to define helper class with __call__ method:
class X:
def __init__(self,s): self.s=s
def __call__(self): return self.s.upper()
l=]
|
by: a |
last post by:
can someone tell me how to use them
thanks
| |
by: Gregory Guthrie |
last post by:
Sorry for a simple question- but I don't understand how to parse this use of
a list comprehension.
The "or" clauses are odd to me.
It also seems like it is being overly clever (?) in using a lc expression as
a for loop to drive the recursion.
Thanks for any insight!
Gregory
|
by: bullockbefriending bard |
last post by:
Given:
class Z(object):
various defs, etc.
class ZList(list):
various defs, etc.
i would like to be able to replace
|
by: beginner |
last post by:
Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
|
by: Debajit Adhikary |
last post by:
I have two lists:
a =
b =
What I'd like to do is append all of the elements of b at the end of
a, so that a looks like:
a =
|
by: beginner |
last post by:
Hi All,
If I have a list comprehension:
ab=
c = "ABC"
print c
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |