Numeric and matlab
Schüle Daniel
uval at rz.uni-karlsruhe.de
Sun Feb 5 21:40:55 EST 2006
Hello,
[...]
>
> I'm sure there are more, but these jump out at me as I'm going. It
> seems as if the idx=find() stuff can be done with Numeric.nonzeros(),
> but you can't index with that, like
>
> a=Numeric.arange(1,11,1)
> idx=Numeric.nonzeros(a)
import Numeric as N
N.nonzero
without s :)
> a=a[idx] % doesn't work
i don't know whether arange object overloads __getitem__
like
>>> class X(object):
... def __getitem__(self,i):
... return self.lst[i]
... def __init__(self):
... self.lst = [7,5,3,1]
...
>>> x=X()
>>> x[0]
7
>>> x[1]
5
>>>
or consider
>>> class List(list):
... def __getitem__(self,idx):
... if type(idx) is list:
... ret = []
... lst = list(self)
... return [lst[i] for i in range(len(lst)) if i in idx]
...
>>> lst=List([1,2,3,4,5])
>>> lst
[1, 2, 3, 4, 5]
>>> lst[[0,2]]
[1, 3]
>>>
if it would overload this would be possible, the normal
python list takes slices
range(0,100)[1:10:2]
s = slice(1,10,2)
range(0,100)[s]
here some ideas for what you tring to do
>>> nums # normal python list
[0, 1, 2, 1, 0, 0, 0, 3, 4, 0]
>>> filter(lambda x: x!=0, nums)
[1, 2, 1, 3, 4]
>>> [item for item in nums if item !=0 ]
[1, 2, 1, 3, 4]
>>>
>>> a=N.arange(0,11,.5)
>>> filter(lambda x: x!=0, a)
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0,
7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5]
>>> [item for item in a if item !=0 ]
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0,
7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5]
Regards, Daniel
More information about the Python-list
mailing list