[changing the subject; was: 'where' statement in Python?]<div><br></div><div>I think this is an interesting idea (whether worth adding is a different question). I think it would be confusing that</div><div><font class="Apple-style-span" face="'courier new', monospace">   a[x] = (y,z)</font></div>

<div>does something entirely different when x is 1 or (1,2). If python *were* to add something like this, I think perhaps a different syntax should be considered:</div><div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">a[[x]] = y</font></div>

<div><font class="Apple-style-span" face="'courier new', monospace">y = a[[x]]</font></div><div><br></div><div>which call <font class="Apple-style-span" face="'courier new', monospace">__setitems__</font> and <span class="Apple-style-span" style="font-family: 'courier new', monospace; ">__getitems__</span> respectively. This makes it clear that something different is going on and eliminates the ambiguity for dicts.</div>

<div><br></div><div>--- Bruce<br><a href="http://www.vroospeak.com" target="_blank">http://www.vroospeak.com</a><br><a href="http://google-gruyere.appspot.com" target="_blank">http://google-gruyere.appspot.com</a><br><br>


<br><br><div class="gmail_quote">On Tue, Jul 20, 2010 at 10:18 AM, Sturla Molden <span dir="ltr"><<a href="mailto:sturla@molden.no">sturla@molden.no</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

So I'd rather speak of something useful instead: NumPy's "Fancy indexing".<br>
<br>
"Fancy indexing" (NumPy jargon) will in this context mean that we allow indexes to be an iterable, not just integers:<br>
<br>
   mylist[(1,2,3)] ==  mylist[1,2,3]<br>
   mylist[iterable] == [a(i) for i in iterable]<br>
<br>
That is what NumPy and Matlab do, as well as Fortran 90 (and certain C++ libraries such as Blitz++). It has all the power of the "where keyword", while being more flexible to use, and intention is more explicit. It is also well tested syntax.<br>


<br>
Thus with "fancy indexing":<br>
<br>
   alist[iterable] == [alist[i] for i in iterable]<br>
<br>
That is what we really need!<br>
<br>
Note that this is not a language syntax change, it is just a change of how __setitem__ and __getitem__ works for certain container types. NumPy already does this, so the syntax itself is completely valid Python. And as for "where", it is just a function.<br>


<br>
Andrey's proposed where keyword is a crippled tool in comparison. That is, the real power of a list of indexers is that it can be obtained and manipulated with any conceivable method, e.g. slicing. It also allows numpy to have an "argsort" function, since an index list can be reused on multiple arrays:<br>


<br>
   idx = np.argsort(array_a)<br>
   sorteda  = array_a[idx]<br>
   sortedb = array_b[idx]<br>
<br>
is the same as<br>
<br>
   tmp = sorted([a,i for i,a in enumerate(lista)])<br>
   sorteda = [a for a,i in tmp]<br>
   sortedb = [listb[i] for a,i in tmp]<br>
<br>
Which is the more readable?<br>
<br>
Implementing a generic "where function" can be achieved with a lambda:<br>
<br>
   idx = where(lambda x:  x== 47, alist)<br>
<br>
or a list comprehension (this would be very similar to NumPy):<br>
<br>
   idx = where([x==47 for x in alist])<br>
<br>
But to begin with, I think we should get NumPy style "fancy indexing" to standard container types like list, tuple, string, bytes, bytearray, array and deque. That would just be a handful of subclasses, and I think they should (initially) be put in a standard library module, and possibly replace the current cointainers in Python 4000.<br>


<br>
But as for a where keyword: My opinion is a big -1, if  I have the right to vote. We should rather implement a where function and overload the mentioned container types. The where function should go in the same module.<br>


<br>
So all in all, I am +1 for a "where module" and -1 for a "where keyword".<br>
<br>
P.S. I'll admit that dict and set might add to some confusion, since "fancy indexing" would be ambigous for them.<br>
<br>
Regards,<br><font color="#888888">
Sturla</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div>