"where" in python
Chris
chris.morisset at gmail.com
Thu Jun 4 13:53:23 EDT 2009
I am a newby in Python and I'm first looking for equivalent to things
I already manage: IDL.
For example, I want to plot a sub-set of points, selected from a
bigger set by applying some filter. In my example, I want to select
only the values > 0.
I succeed to write 5 different ways to do this, which one is the more
efficient, the quicker, the more Python?
It seems the 4th method give wrong results, but I don't know why...
Thanks for your tips,
Christophe
------------------------------------------------------------------------------------------
import pylab as pylab
import numpy as numpy
#Read the data
d=pylab.load('Tabla_General2.cvs',comments='#',delimiter=';')
# Define the columns to plot
i_x = 10
i_y = 5
# Select the points to plot, 1rst method
b_where = (d[:,i_x]>0) & (d[:,i_y]>0)
xb = d[b_where,i_x]
yb = d[b_where,i_y]
# 2nd method
xb2=pylab.compress(b_where,d[:,i_x])
yb2=pylab.compress(b_where,d[:,i_y])
# 3rd method
i_where = pylab.where((d[:,i_x]>0) & (d[:,i_y]>0),1,0)
xi = d[i_where,i_x]
yi = d[i_where,i_y]
# 4th method
xi2=pylab.compress(i_where,d[:,i_x])
yi2=pylab.compress(i_where,d[:,i_y])
#5th method
in_where = numpy.transpose(numpy.where((d[:,i_x]>0) & (d[:,i_y]>0)))
xin = d[in_where,i_x]
yin = d[in_where,i_y]
------------------------------------------------------------------------------
More information about the Python-list
mailing list