Re: List comprehension vs filter()

2011-04-20 Thread Ian Kelly
On Tue, Apr 19, 2011 at 9:59 PM, Chris Angelico ros...@gmail.com wrote: On Wed, Apr 20, 2011 at 1:45 PM, Chris Rebert c...@rebertia.com wrote: Built-ins aren't quite the same as globals, but essentially yes: Sure. That might explain some of the weirdness, but it doesn't explain why things

Re: List comprehension vs filter()

2011-04-20 Thread Chris Angelico
On Wed, Apr 20, 2011 at 4:08 PM, Ian Kelly ian.g.ke...@gmail.com wrote: What is the scope the code is running in?  If this is part of a class definition, that could explain why the lambda is not seeing the type / posttype closure: because there isn't one. It's inside an if, but that's all. The

Re: List comprehension vs filter()

2011-04-20 Thread Tim Roberts
Chris Angelico ros...@gmail.com wrote: On Wed, Apr 20, 2011 at 1:45 PM, Chris Rebert c...@rebertia.com wrote: Built-ins aren't quite the same as globals, but essentially yes: Sure. That might explain some of the weirdness, but it doesn't explain why things were still weird with the variable

Re: List comprehension vs filter()

2011-04-20 Thread Chris Angelico
On Wed, Apr 20, 2011 at 5:16 PM, Tim Roberts t...@probo.com wrote: It's because, unlike some other languages (like Pascal), Python doesn't have infinitely recursive nested namespaces.  Glossing over details, there is a global namespace, and there is a local namespace.  A new function gets a

Re: List comprehension vs filter()

2011-04-20 Thread Steven D'Aprano
On Wed, 20 Apr 2011 13:10:21 +1000, Chris Angelico wrote: Context: Embedded Python interpreter, version 2.6.6 I have a list of dictionaries, where each dictionary has a type element which is a string. I want to reduce the list to just the dictionaries which have the same type as the first

Re: List comprehension vs filter()

2011-04-20 Thread Peter Otten
Chris Angelico wrote: Context: Embedded Python interpreter, version 2.6.6 I have a list of dictionaries, where each dictionary has a type element which is a string. I want to reduce the list to just the dictionaries which have the same type as the first one.

Re: List comprehension vs filter()

2011-04-20 Thread Chris Angelico
On Wed, Apr 20, 2011 at 8:16 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: There's your problem. IDEs often play silly buggers with the environment in order to be clever. You've probably found a bug in whatever IDE you're using. And this is why I won't touch the buggers with

Re: List comprehension vs filter()

2011-04-20 Thread Mel
Chris Angelico wrote: On Wed, Apr 20, 2011 at 5:16 PM, Tim Roberts t...@probo.com wrote: You can solve this through the common lamba idiom of a closure: lst=filter(lambda x,posttype=posttype: x[type].lower()==posttype,lst) Seems a little odd, but sure. I guess this means that a function's

Re: List comprehension vs filter()

2011-04-20 Thread Ian Kelly
On Wed, Apr 20, 2011 at 4:41 AM, Peter Otten __pete...@web.de wrote: The assignment writes to the local namespace, the lambda function reads from the global namespace; this will only work as expected if the two namespaces are the same: exec type = 42; print filter(lambda x: x == type, [42])

Re: List comprehension vs filter()

2011-04-20 Thread Chris Angelico
On Thu, Apr 21, 2011 at 12:44 AM, Ian Kelly ian.g.ke...@gmail.com wrote: So, the question for the OP:  Is this file being run with execfile? Not execfile per se; the code is fetched from the database and then executed with: PyObject *v=PyRun_StringFlags(code,Py_file_input,py_globals,locals,0);

Re: List comprehension vs filter()

2011-04-20 Thread Ian Kelly
On Wed, Apr 20, 2011 at 12:03 PM, Chris Angelico ros...@gmail.com wrote: On Thu, Apr 21, 2011 at 12:44 AM, Ian Kelly ian.g.ke...@gmail.com wrote: So, the question for the OP:  Is this file being run with execfile? Not execfile per se; the code is fetched from the database and then executed

List comprehension vs filter()

2011-04-19 Thread Chris Angelico
Context: Embedded Python interpreter, version 2.6.6 I have a list of dictionaries, where each dictionary has a type element which is a string. I want to reduce the list to just the dictionaries which have the same type as the first one. lst=[{type:calc,...},{type:fixed,...},{type:calc,...},...]

Re: List comprehension vs filter()

2011-04-19 Thread Chris Angelico
On Wed, Apr 20, 2011 at 1:10 PM, Chris Angelico ros...@gmail.com wrote: type=lst[0][type].lower() lst=filter(lambda x: x[type].lower()==type,lst) # Restrict to that one type After posting, I realised that type is a built-in identifier, and did a quick variable name change to posttype which

Re: List comprehension vs filter()

2011-04-19 Thread Chris Rebert
On Tue, Apr 19, 2011 at 8:10 PM, Chris Angelico ros...@gmail.com wrote: snip type=lst[0][type].lower() Tangent: Don't call it type; you're shadowing the built-in class of the same name. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list

Re: List comprehension vs filter()

2011-04-19 Thread Chris Angelico
On Wed, Apr 20, 2011 at 1:22 PM, Chris Rebert c...@rebertia.com wrote: On Tue, Apr 19, 2011 at 8:10 PM, Chris Angelico ros...@gmail.com wrote: snip type=lst[0][type].lower() Tangent: Don't call it type; you're shadowing the built-in class of the same name. By shadowing you mean that the

Re: List comprehension vs filter()

2011-04-19 Thread Chris Rebert
On Tue, Apr 19, 2011 at 8:23 PM, Chris Angelico ros...@gmail.com wrote: On Wed, Apr 20, 2011 at 1:22 PM, Chris Rebert c...@rebertia.com wrote: On Tue, Apr 19, 2011 at 8:10 PM, Chris Angelico ros...@gmail.com wrote: snip type=lst[0][type].lower() Tangent: Don't call it type; you're shadowing

Re: List comprehension vs filter()

2011-04-19 Thread Chris Angelico
On Wed, Apr 20, 2011 at 1:45 PM, Chris Rebert c...@rebertia.com wrote: Built-ins aren't quite the same as globals, but essentially yes: Sure. That might explain some of the weirdness, but it doesn't explain why things were still weird with the variable named posttype. However, since the list