Property testing in lists
Dan Brown
danb at alum.mit.edu
Tue Sep 12 15:10:53 EDT 2000
Hi there,
I'm a python newbie (my work is mostly in scientific computing, so I use
Matlab for most of my coding needs).
I'd like to be able to identify the indexes of all members of a list
that
have a particular property. Certainly, there's lots of ways to do it;
here's two examples with testing for being one more than a multiple of
4.
>>> N = [1, 10, 194, 148, -403]
>>> map (lambda x: x[1],filter (lambda x: x[0] % 4 == 1, map (None, N,
range(len(N)))))
[0, 4]
>>> index = []
>>> for i in range (len(N)):
... if (N[i] % 4 == 1):
... index.append(i)
...
>>> index
[0, 4]
However, these are both much more disgusting than the equivalent Matlab
code: [Lists in Matlab are indexed starting at 1.]
>> N = [1 10 194 148 -403];
>> find (mod (N,4) == 1)
ans =
1 5
Is there an equivalent form in Python which is pretty like the Matlab
code,
or am I just barking up the wrong tree?
Thanks much!
-dan brown
danb at alum.mit.edu
More information about the Python-list
mailing list