Re: Iterating across a filtered list

2007-03-14 Thread Bruno Desthuilliers
Paul Rubin a écrit : Bruno Desthuilliers [EMAIL PROTECTED] writes: (snip) Python has had functions as first class objects and (quite-limited-but) anonymous functions, map(), filter() and reduce() as builtin funcs at least since 1.5.2 (quite some years ago). True, though no iterators so you

Re: Iterating across a filtered list

2007-03-14 Thread Bruno Desthuilliers
Arnaud Delobelle a écrit : On Mar 13, 8:53 pm, Bruno Desthuilliers [EMAIL PROTECTED] wrote: Paul Rubin a écrit : [snip] Iterators like that are a new Python feature List comps are not that new (2.0 or 2.1 ?): print \n.join([contact for name, contact in contacts.items() \

Iterating across a filtered list

2007-03-13 Thread Drew
All - I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list. My code is working just fine, but I'm wondering if this is the most elegant way to do this.

Re: Iterating across a filtered list

2007-03-13 Thread Paul Rubin
Drew [EMAIL PROTECTED] writes: I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list. My code is working just fine, but I'm wondering if this is the most elegant

Re: Iterating across a filtered list

2007-03-13 Thread Drew
On Mar 13, 2:42 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: If I can decipher your Ruby example (I don't know Ruby), I think you want: for name,contact in contacts.iteritems(): if re.search('search', name): print contact If you just want to filter the dictionary

Re: Iterating across a filtered list

2007-03-13 Thread Arnaud Delobelle
On Mar 13, 6:04 pm, Drew [EMAIL PROTECTED] wrote: All - Hi! [snip] http://pastie.caboo.se/46647 There is no need for such a convoluted list comprehension as you iterate over it immediately! It is clearer to put the filtering logic in the for loop. Moreover you recalculate the regexp for each

Re: Iterating across a filtered list

2007-03-13 Thread Paul Rubin
Drew [EMAIL PROTECTED] writes: You're exactly on the mark. I guess I was just wondering if your first example (that is, breaking the if statement away from the iteration) was preferred rather than initially filtering and then iterating. I think the multiple statement version is more in Python

Re: Iterating across a filtered list

2007-03-13 Thread Paul Rubin
Arnaud Delobelle [EMAIL PROTECTED] writes: in the for loop. Moreover you recalculate the regexp for each element of the list. The re library caches the compiled regexp, I think. -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterating across a filtered list

2007-03-13 Thread Bruno Desthuilliers
Paul Rubin a écrit : Drew [EMAIL PROTECTED] writes: You're exactly on the mark. I guess I was just wondering if your first example (that is, breaking the if statement away from the iteration) was preferred rather than initially filtering and then iterating. I think the multiple statement

Re: Iterating across a filtered list

2007-03-13 Thread Arnaud Delobelle
On Mar 13, 7:36 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Arnaud Delobelle [EMAIL PROTECTED] writes: in the for loop. Moreover you recalculate the regexp for each element of the list. The re library caches the compiled regexp, I think. That would surprise me. How can re.search know

Re: Iterating across a filtered list

2007-03-13 Thread Paul Rubin
Bruno Desthuilliers [EMAIL PROTECTED] writes: I don't know if I qualify as a Python traditionalist, but I'm using Python since the 1.5.2 days, and I usually favor list comps or generator expressions over old-style loops when it comes to this kind of operations. I like genexps when they're

Re: Iterating across a filtered list

2007-03-13 Thread Arnaud Delobelle
On Mar 13, 8:53 pm, Bruno Desthuilliers [EMAIL PROTECTED] wrote: Paul Rubin a écrit : [snip] Iterators like that are a new Python feature List comps are not that new (2.0 or 2.1 ?): print \n.join([contact for name, contact in contacts.items() \ if search.match(name)])

Re: Iterating across a filtered list

2007-03-13 Thread Gabriel Genellina
En Tue, 13 Mar 2007 15:04:50 -0300, Drew [EMAIL PROTECTED] escribió: I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list. My code is working just fine, but

Re: Iterating across a filtered list

2007-03-13 Thread Arnaud Delobelle
On Mar 13, 8:59 pm, Gabriel Genellina [EMAIL PROTECTED] wrote: [snip] def find(self, search): search_re = re.compile(search, re.IGNORECASE) for result in [self.contacts[name] for name in self.contacts if search_re.match(name)]: print result I do not see how for y in

Re: Iterating across a filtered list

2007-03-13 Thread Gabriel Genellina
En Tue, 13 Mar 2007 17:19:53 -0300, Arnaud Delobelle [EMAIL PROTECTED] escribió: On Mar 13, 7:36 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: The re library caches the compiled regexp, I think. That would surprise me. How can re.search know that string.lower(search) is the same each

Re: Iterating across a filtered list

2007-03-13 Thread Gabriel Genellina
En Tue, 13 Mar 2007 18:16:32 -0300, Arnaud Delobelle [EMAIL PROTECTED] escribió: On Mar 13, 8:59 pm, Gabriel Genellina [EMAIL PROTECTED] wrote: [snip] def find(self, search): search_re = re.compile(search, re.IGNORECASE) for result in [self.contacts[name] for name in

Re: Iterating across a filtered list

2007-03-13 Thread Arnaud Delobelle
On Mar 13, 9:31 pm, Gabriel Genellina [EMAIL PROTECTED] wrote: En Tue, 13 Mar 2007 17:19:53 -0300, Arnaud Delobelle [EMAIL PROTECTED] escribió: On Mar 13, 7:36 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: The re library caches the compiled regexp, I think. That would surprise me.

Iterating across a filtered list

2007-03-13 Thread Łukasz Ligowski
Hi, On Tuesday 13 of March 2007 22:16:32 Arnaud Delobelle wrote: for x in L: if g(x): do stuff with f(x) for x in itertools.ifilterfalse(g, L): do stuff Maybe this would be even better? L -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterating across a filtered list

2007-03-13 Thread Gabriel Genellina
En Tue, 13 Mar 2007 19:12:12 -0300, Arnaud Delobelle [EMAIL PROTECTED] escribió: py import re py x = re.compile(ijk) py y = re.compile(ijk) py x is y True Both, separate calls, returned identical results. You can show the cache: OK I didn't realise this. But even so each time there