Newbie question:
I have been generally open to the proposal that list comprehensions
should replace 'map', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use 'zip' because I'll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without 'map'?)
Thanks,
Alan Isaac
Jul 27 '05
22 1900
Me: Here's a clever, though not (in my opinion) elegant solution
... This seems a bit more elegant, though the "replace" dictionary is still a bit of a hack
Here's the direct approach without using itertools. Each list is
iterated over only once. No test against a sequence element is ever
made (either as == or 'is') and the end of the sequence exception
is raised only once per input iterator.
The use of a list for the flag is a bit of a hack. If the list has
1 element then its true, no elements then its false. By doing it this
way I don't need one extra array and one extra indexing/enumeration.
def zipfill(*seqs):
count = len(seqs)
seq_info = [(iter(seq), [1]) for seq in seqs]
while 1:
fields = []
for seq, has_data in seq_info:
if has_data:
try:
fields.append(s eq.next())
except StopIteration:
fields.append(N one)
del has_data[:]
count -= 1
else:
fields.append(N one)
if count:
yield fields
else:
break
Hmm, it should probably yield tuple(fields)
Andrew da***@dalkescie ntific.com
[Paolino] Well, for my little experiences use cases in which the lists have different lengths are rare, but in those cases I don't see the reason of not being able to zip to the longest one.What is really strange is that I have to use map(None,....) for that,instead of another zip-like function which ,at least would be intutitive for the average user.Also map(None,...) looks like a super-hack and it's not elegant or readable or logic (IMO)
I think zip comes to substitute the tuple.__new__ untolerant implementation. A dumb like me wuold expect map(tuple,[1,2,3],[2,3,4]) to work, so pretending map(None,....) would do it is like saying that None and tuple are near concepts, which is obviously an absurdity.
Yes, map(None, ...) lacks grace and it would be nice if it had never
been done. The more recently implemented zip() does away with these
issues. The original was kept for backwards compatibility. That's
evolution.
My sense for the rest is that your difficulties arise from fighting the
language rather than using it as designed. Most language features are
the result of much deliberation. When design X was chosen over
alternative Y, it is a pretty good cue that X is a more harmonious way
to do things.
Some other languages chose to implement both X and Y. On the plus
side, your intuition likely matches one of the two. On the minus side,
someone else's intuition may not match your own. Also, it leads to
language bloat. More importantly, such a language provides few cues as
to how to select components that work together harmoniously.
Unfortunately, that makes it effortless to mire yourself in deep goo.
My advice is to use the language instead of fighting it. Guido has
marked the trail; don't ignore the signs unless you really know where
you're going.
Raymond
"... and soon you'll feel right as rain." -- from The Matrix
Andrew Dalke wrote: Steven Bethard wrote:
Here's one possible solution:
py> import itertools as it py> def zipfill(*lists) : ... max_len = max(len(lst) for lst in lists)
A limitation to this is the need to iterate over the lists twice, which might not be possible if one of them is a file iterator.
Here's a clever, though not (in my opinion) elegant solution
import itertools
def zipfill(*seqs): count = [len(seqs)] def _forever(seq): for item in seq: yield item count[0] -= 1 while 1: yield None seqs = [_forever(seq) for seq in seqs] while 1: x = [seq.next() for seq in seqs] if count == [0]: break yield x
I like this solution best (note, it doesn't actually use itertools). My
naive solution:
def lzip(*args):
ilist = [iter(a) for a in args]
while 1:
res = []
count = 0
for i in ilist:
try:
g = i.next()
count += 1
except StopIteration: # End of iter
g = None
res.append(g)
if count > 0: # At least one iter wasn't finished
yield tuple(res)
else: # All finished
raise StopIteration
Christopher Subich wrote: My naive solution:
... for i in ilist: try: g = i.next() count += 1 except StopIteration: # End of iter g = None
...
What I didn't like about this was the extra overhead of all
the StopIteration exceptions. Eg,
zipfill("a", range(1000))
will raise 1000 exceptions (999 for "a" and 1 for the end of the range).
But without doing timing tests I'm not sure which approach is
fastest, and it may depend on the data set.
Since this is code best not widely used, I don't think it's something
anyone should look into either. :)
Andrew da***@dalkescie ntific.com
Andrew Dalke wrote: Steven Bethard wrote: Here's one possible solution:
py> import itertools as it py> def zipfill(*lists) : ... max_len = max(len(lst) for lst in lists) A limitation to this is the need to iterate over the lists twice, which might not be possible if one of them is a file iterator.
Here's a clever, though not (in my opinion) elegant solution
import itertools
def zipfill(*seqs): count = [len(seqs)] def _forever(seq): for item in seq: yield item count[0] -= 1 while 1: yield None seqs = [_forever(seq) for seq in seqs] while 1: x = [seq.next() for seq in seqs] if count == [0]: break yield x
This seems a bit more elegant, though the "replace" dictionary is still a bit of a hack
from itertools import repeat, chain, izip
sentinel = object() end_of_stream = repeat(sentinel )
def zipfill(*seqs): replace = {sentinel: None}.get seqs = [chain(seq, end_of_stream) for seq in seqs] for term in izip(*seqs): for element in term: if element is not sentinel: break else: # All sentinels break
yield [replace(element , element) for element in term]
Combining your "clever" and your "elegant" approach to something fast
(though I'm not entirely confident it's correct):
def fillzip(*seqs):
def done_iter(done=[len(seqs)]):
done[0] -= 1
if not done[0]:
return
while 1:
yield None
seqs = [chain(seq, done_iter()) for seq in seqs]
return izip(*seqs)
Whether we ran out of active sequences is only tested once per sequence.
Fiddling with itertools is always fun, but feels a bit like reinventing the
wheel in this case. The only excuse being that you might need a lazy
map(None, ...) someday...
Peter
Peter Otten wrote: Combining your "clever" and your "elegant" approach to something fast (though I'm not entirely confident it's correct):
def fillzip(*seqs): def done_iter(done=[len(seqs)]): done[0] -= 1 if not done[0]: return while 1: yield None seqs = [chain(seq, done_iter()) for seq in seqs] return izip(*seqs)
Ohh, that's pretty neat passing in 'done' via a mutable default argument.
It took me a bit to even realize why it does work. :)
Could make it one line shorter with
from itertools import chain, izip, repeat
def fillzip(*seqs):
def done_iter(done=[len(seqs)]):
done[0] -= 1
if not done[0]:
return []
return repeat(None)
seqs = [chain(seq, done_iter()) for seq in seqs]
return izip(*seqs)
Go too far on that path and the code starts looking likg
from itertools import chain, izip, repeat
forever, table = repeat(None), {0: []}.get
def fillzip(*seqs):
def done_iter(done=[len(seqs)]):
done[0] -= 1
return table(done[0], forever)
return izip(*[chain(seq, done_iter()) for seq in seqs])
Now add the performance tweak....
def done_iter(done=[len(seqs)], forever=forever , table=table)
Okay, I'm over it. :)
Andrew da***@dalkescie ntific.com
Peter Otten wrote: def fillzip(*seqs): def done_iter(done=[len(seqs)]): done[0] -= 1 if not done[0]: return while 1: yield None seqs = [chain(seq, done_iter()) for seq in seqs] return izip(*seqs)
Can I play too? How about :
import itertools
def fillzip(*seqs):
def Nones(countacti ve=[len(seqs)]):
countactive[0] -= 1
while countactive[0]:
yield None
seqs = [itertools.chain (seq, Nones()) for seq in seqs]
return itertools.izip( *seqs)
--Scott David Daniels Sc***********@A cm.Org
Andrew Dalke wrote: Peter Otten wrote: Combining your "clever" and your "elegant" approach to something fast (though I'm not entirely confident it's correct):
def fillzip(*seqs): def done_iter(done=[len(seqs)]): done[0] -= 1 if not done[0]: return while 1: yield None seqs = [chain(seq, done_iter()) for seq in seqs] return izip(*seqs) Ohh, that's pretty neat passing in 'done' via a mutable default argument.
It took me a bit to even realize why it does work. :)
Though I would never have come up with it, were it not for the juxtaposition
of your two variants (I initially disliked the first and tried to improve
on the second), it is an unobvious merger :)
It's a bit fragile, too, as
Could make it one line shorter with
from itertools import chain, izip, repeat def fillzip(*seqs): def done_iter(done=[len(seqs)]): done[0] -= 1 if not done[0]: return [] return repeat(None) seqs = [chain(seq, done_iter()) for seq in seqs] return izip(*seqs)
that won't work because done_iter() is now no longer a generator.
In effect you just say
seqs = [chain(seq, repeat(None)) for seq in seqs[:-1]] + [chain(seq[-1],
[])]
I tried
class Done(Exception) :
pass
pad = repeat(None)
def fillzip(*seqs):
def check(active=[len(seqs)]):
active[0] -= 1
if not active[0]:
raise Done
# just to turn check() into a generator
if 0: yield None
seqs = [chain(seq, check(), pad) for seq in seqs]
try
for item in izip(*seqs):
yield item
except Done:
pass
to be able to use the faster repeat() instead of the while loop, and then
stared at it for a while -- in vain -- to eliminate the for item... loop.
If there were a lazy ichain(iter_of_ iters) you could tweak check() to decide
whether a repeat(None) should follow it, but I'd rather not ask Raymond for
that particular addition to the itertools.
Now add the performance tweak....
def done_iter(done=[len(seqs)], forever=forever , table=table)
Okay, I'm over it. :)
Me too. I think. For now...
Peter
Me: Could make it one line shorter with from itertools import chain, izip, repeat def fillzip(*seqs): def done_iter(done=[len(seqs)]): done[0] -= 1 if not done[0]: return [] return repeat(None) seqs = [chain(seq, done_iter()) for seq in seqs] return izip(*seqs)
Peter Otten: that won't work because done_iter() is now no longer a generator. In effect you just say
seqs = [chain(seq, repeat(None)) for seq in seqs[:-1]] + [chain(seq[-1], [])]
It does work - I tested it. The trick is that izip takes iter()
of the terms passed into it. iter([]) -> an empty iterator and
iter(repeat(Non e)) -> the repeat(None) itself.
'Course then the name should be changed.
Andrew da***@dalkescie ntific.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Guy Robinson |
last post by:
Hello,
Trying to change a string(x,y values) such as :
s = "114320,69808 114272,69920 113568,71600 113328,72272"
into (x,-y):
out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"
|
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: Mahesh Padmanabhan |
last post by:
Hi,
When list comprehension was added to the language, I had a lot of
trouble understanding it but now that I am familiar with it, I am not
sure how I programmed in Python without it.
Now I see that generator expressions have been added to the language
with 2.4 and I question the need for it. I know that it allows for lazy
evaluation which speeds things up for larger lists but why was it
necessary to add it instead of improving list...
|
by: Matt Gerrans |
last post by:
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...
|
by: Mike Meyer |
last post by:
Ok, we've added list comprehensions to the language, and seen that
they were good. We've added generator expressions to the language, and
seen that they were good as well.
I'm left a bit confused, though - when would I use a list comp instead
of a generator expression if I'm going to require 2.4 anyway?
Thanks,
<mike
--
| |
by: Steven Bethard |
last post by:
George Sakkis wrote:
> "Steven Bethard" <steven.bethard@gmail.com> wrote:
>> Dict comprehensions were recently rejected:
>> http://www.python.org/peps/pep-0274.html
>> The reason, of course, is that dict comprehensions don't gain you
>> much at all over the dict() constructor plus a generator expression,
>> e.g.:
>> dict((i, chr(65+i)) for i in range(4))
>
> Sure, but the same holds for list comprehensions: list(i*i for i in
|
by: Heiko Wundram |
last post by:
Hi all!
The following PEP tries to make the case for a slight unification of for
statement and list comprehension syntax.
Comments appreciated, including on the sample implementation.
===
PEP: xxx
Title: Unification of for-statement and list-comprehension syntax
|
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: Pat |
last post by:
I have written chunks of Python code that look this:
new_array =
for a in array:
if not len( a ):
continue
new_array.append( a )
and...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: 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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |