I don't know if this has been talked about, I
couldn't find it in the PEPs, newsgroup or web site.
I use PV-WAVE, a matrix manipulation language similar to
MatLab and IDL. PV-WAVE and IDL have the same ancestry.
Python, along with arrays from Numeric Python makes a
pretty good substitute for PV-WAVE. Plus there are many
simularities in syntax between Python and PV-WAVE.
PV-WAVE code:
PV-WAVE> a=[10,20,30,40,50,60,70,80,90]
PV-WAVE> b=[0,4,5,7]
PV-WAVE> c=a(b)
PV-WAVE> print,c
[10, 50, 60, 80]
Non-contiguous indexing or is it non-contiguous slicing?
Actually it should probably be called dicing. :-)
Anyway, I would really like that for Python, so here
goes...
I find myself doing the following to replicate PV-WAVE's
behavoir:
>>> a=[10,20,30,40,50,60,70,80,90]
>>> b=[0,4,5,7]
>>> c=[]
>>> for i in b:
... c.append(a[i])
...
>>> c
[10, 50, 60, 80]
First, just taking the idea from PV-WAVE, for example:
>>> c=a[0,4,5,7]
>>> c
[10,50,60,80]
Extending it to include ranges:
>>> a[0:3,6:-1]
[10,20,30,70,80]
Maybe overlapping ranges and mix of indexing and
slicing:
>>> a[0:4,3:4,0:-1,-1]
[10,20,30,40,40,10,20,30,40,50,60,70,80,90]
There are some issues:
The comma is used in Numerical Python to seperate the
array coordinates, so a[0:3,6:-1] is already a
legitimate Numerical Python statement.
It isn't very pretty.
Maybe have a 'dice()' function?
>>> a=[10,20,30,40,50,60,70,80,90]
>>> b=[0,4,5,7]
>>> c=a.dice(b) # or c=dice(a,b) ?
>>> c
[10,50,60,80]
Thanks for Python!
|