Matlab-style mask in Python??

Uwe Schmitt uwe at rocksport.de
Tue Oct 30 13:22:02 CET 2001


Ozone Hole near South Pole <ozonehole2k at yahoo.com> wrote:
| Hi,

| I am a long time Matlab user.  I just picked up Python for a short
| while.
| I wonder what should be the best way to do sth like:

| Matlab:
| a=[3,4,6,7,2,54,2,1,2]
| idx=a>4      ans: [0,0,1,1,0,1,0,0,0]
| a(idx)         ans: [6,7,54]

| Python:
| a=[3,4,6,7,2,54,2,1,2]
| idx=map(lambda x: x>4,a) #assume this calculation is lengthy
|                          #and we don't want to repeat
| last cmd???

| While we can always put that into a for loop, then append the ans
| element-by-element....  I feel there should be an easier way in
| Python.
| Any suggestions?

have a look at Numerical Python http://www.numpy.org. I think it
will help you.

Greetings, Uwe.

-- 
Uwe.Schmitt at num.uni-sb.de      Universität des Saarlandes
phone: +49 (0)681/302-2468     Geb. 36.1, Zi. 4.17, PF 151150
                               D-66041  Saarbrücken
http://www.rocksport.de        http://www.rocksport.de/first_ride.mp3




More information about the Python-list mailing list