The obvious approach using equal doesn't seem to work. I'll forward your request. -- Paul -----Original Message----- From: Sam Tannous [mailto:stannous@cisco.com] Sent: Wednesday, March 14, 2001 3:31 AM To: Paul F. Dubois Subject: numpy array matching Paul, I can't seem to get my question posted to the discussion list. Could you please post it for me? (Or even answer it for me?) Thanks, Sam ------------------------------------------- If I have a character array t:
t = array(['abcd','aacd','ddfa','qqcd']) print t [[a b c d] [a a c d] [d d f a] [q q c d]]
I'm trying to find the row numbers that have a 'c' in the third column (and I'm trying to avoid a 'for' loop). What's the most efficient way to create an array that contains the indexes of the rows that match a particular pattern? (using masks?) The answer should be an array ([0,1,3]) Thanks for any tips, Sam
"Paul F. Dubois" wrote:
------------------------------------------- If I have a character array t:
t = array(['abcd','aacd','ddfa','qqcd']) print t [[a b c d] [a a c d] [d d f a] [q q c d]]
I'm trying to find the row numbers that have a 'c' in the third column (and I'm trying to avoid a 'for' loop).
What's the most efficient way to create an array that contains the indexes of the rows that match a particular pattern? (using masks?)
The answer should be an array ([0,1,3])
The obvious approach using equal doesn't seem to work. I'll forward your request.
It seems that equal() does not support the char type (anyone know why not?):
equal(t,'c') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function not supported for these types, and can't coerce to supported types
The following is kind of an ugly kludge, but it does work:
from Numeric import *
t = array(['abcd','aacd','ddfa','qqcd'])
t2 = t.astype(UnsignedInt8)
nonzero(equal(t2,ord('c'))[:,2])
array([0, 1, 3]) -Chris -- Christopher Barker, Ph.D. cbarker@jps.net --- --- --- http://www.jps.net/cbarker -----@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Water Resources Engineering ------ @ ------ @ ------ @ Coastal and Fluvial Hydrodynamics ------- --------- -------- ------------------------------------------------------------------------ ------------------------------------------------------------------------
Chris Barker wrote:
t = array(['abcd','aacd','ddfa','qqcd'])
t2 = t.astype(UnsignedInt8)
nonzero(equal(t2,ord('c'))[:,2])
array([0, 1, 3])
OOPS. For a little better performance:
nonzero(equal(t2[:,2],ord('c')))
array([0, 1, 3]) -- Christopher Barker, Ph.D. cbarker@jps.net --- --- --- http://www.jps.net/cbarker -----@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Water Resources Engineering ------ @ ------ @ ------ @ Coastal and Fluvial Hydrodynamics ------- --------- -------- ------------------------------------------------------------------------ ------------------------------------------------------------------------
participants (2)
-
Chris Barker
-
Paul F. Dubois