[Python-ideas] 'where' statement in Python?

Sturla Molden sturla at molden.no
Tue Jul 20 21:20:32 CEST 2010


Den 20.07.2010 20:16, skrev Guido van Rossum:
> It looks like NumPy's "where" is more like SQL's,

Yes, it is roughtly like a WHERE statement in SQL or Fortran 90, or 
Python's built-in "filter" function (albeit more flexible).

I am not sure I miss "fancy indexing" for Python lists because it is 
useful, or because Fortran have crippled my mind. This is of course easy 
to achieve with two utility functions, also demonstrating what NumPy's 
fancy indexing does. This is with a lambda:

def where(cond, alist):
     return [i for i,a in enumerate(alist) if cond(a)]

def fancyindex(alist, index):
     return [alist[i] for i in index]


> Microsoft's LINQ is also similar to your suggestion.
>    

LINQ is just to compensate for lack of duck-typing in C# ;-) Also when 
used to Pythons list comprehensions, LINQ syntax feels like thinking 
backwards (which can be quite annoying) :-(

Sturla



More information about the Python-ideas mailing list