[Python-ideas] fancy indexing (original) (raw)
Sturla Molden sturla at molden.no
Wed Jul 21 02:03:01 CEST 2010
- Previous message: [Python-ideas] fancy indexing
- Next message: [Python-ideas] Add faster locks to the threading module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Den 21.07.2010 00:51, skrev Mathias Panzenböck:
I'm not sure what this is about but do you mean something like this? >>> l=[1,2,3,4] >>> l[1:2] = ['a','b'] >>> l [1, 'a', 'b', 3, 4]
No, that is slicing. A fancy index is a more flexible slice, as it has no regular structure. It's just a list, tuple or array of indexes, in arbitrary order, possibly repeated.
It would e.g. work like this:
alist = [1,2,3,4] alist[(1,2,1,1,3)] [2, 3, 2, 2, 4]
If know SQL, it means that you can do with indexing what SQL can do with WHERE and JOIN. You can e.g. search a list in O(N) for indexes where a certain condition evaluates to True (cf. SQL WHERE), and then apply these indexes to any list (cf. SQL JOIN).
It is not just for queries, but also for things like sorting. It is what lets NumPy have an "argsort" function. It does not return a sorted array, but an array of indices, which when applied to the array, will return a sorted instance. These indices can in turn be applied to other arrays as well.
Think about what happens when you sort each row in an Excel spreadsheet by the values in a certain column. One column is sorted, the other columns are reordered synchroneously. That is the kind of thing that fancy indexing allows us to do rather easily.
Yes there are other ways of doing this in Python now, but not as elegent I think. And it is not a syntax change to Python (NumPy can do it), it is just a library issue.
This is at least present in NumPy, MATLAB, C# and LINQ, SQL, Fortran 95 (in two ways), Scilab, Octave, and C++ (e.g. Blitz++). The word "fancy indexing" is the name used for it in NumPy.
Sturla
- Previous message: [Python-ideas] fancy indexing
- Next message: [Python-ideas] Add faster locks to the threading module?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]