Hi - I am trying to find a string in a list with strings and have come across the following state of affairs: In [228]: subjects Out[228]: ['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'] In [229]: subjects[0] Out[229]: 'KAA' In [230]: subjects[0] == 'KAA' Out[230]: True In [231]: np.where(subjects == 'KAA') Out[231]: () In [232]: pylab.find(subjects == 'KAA') Out[232]: array([], dtype=int32) It doesn't seem to matter if I make the list into an array: In [233]: np.array(subjects) Out[233]: array(['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'], dtype='|S3') In [234]: pylab.find(subjects == 'KAA') Out[234]: array([], dtype=int32) In [235]: np.where(subjects == 'KAA') Out[235]: () What am I doing wrong? What does it mean that the dtype is IS3? Thanks a bunch -- Ariel
On Mon, Jan 26, 2009 at 13:48, Ariel Rokem <arokem@berkeley.edu> wrote:
Hi - I am trying to find a string in a list with strings and have come across the following state of affairs: In [228]: subjects Out[228]: ['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'] In [229]: subjects[0] Out[229]: 'KAA' In [230]: subjects[0] == 'KAA' Out[230]: True In [231]: np.where(subjects == 'KAA') Out[231]: () In [232]: pylab.find(subjects == 'KAA') Out[232]: array([], dtype=int32)
Well, this will never work. Python lists don't broadcast like numpy arrays.
It doesn't seem to matter if I make the list into an array: In [233]: np.array(subjects) Out[233]: array(['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'], dtype='|S3') In [234]: pylab.find(subjects == 'KAA') Out[234]: array([], dtype=int32) In [235]: np.where(subjects == 'KAA') Out[235]: () What am I doing wrong?
subjects is still a list. You did not assign the array to that name.
What does it mean that the dtype is IS3?
All of your elements are length-3 strings. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Mon, Jan 26, 2009 at 11:48 AM, Ariel Rokem <arokem@berkeley.edu> wrote:
Hi - I am trying to find a string in a list with strings and have come across the following state of affairs: In [228]: subjects Out[228]: ['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'] In [229]: subjects[0] Out[229]: 'KAA' In [230]: subjects[0] == 'KAA' Out[230]: True In [231]: np.where(subjects == 'KAA') Out[231]: () In [232]: pylab.find(subjects == 'KAA') Out[232]: array([], dtype=int32) It doesn't seem to matter if I make the list into an array: In [233]: np.array(subjects) Out[233]: array(['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'], dtype='|S3') In [234]: pylab.find(subjects == 'KAA') Out[234]: array([], dtype=int32) In [235]: np.where(subjects == 'KAA') Out[235]: () What am I doing wrong? What does it mean that the dtype is IS3?
I think you made a typo. Try changing line 233 to subjects = np.array(subjects)
type(subjects) <type 'list'> np.where(subjects == 'KAA') () np.where(np.asarray(subjects) == 'KAA') (array([0]),)
Doh! That's embarrassing! Thanks! On Jan 26, 2009, at 12:00 PM, Keith Goodman wrote:
On Mon, Jan 26, 2009 at 11:48 AM, Ariel Rokem <arokem@berkeley.edu> wrote:
Hi - I am trying to find a string in a list with strings and have come across the following state of affairs: In [228]: subjects Out[228]: ['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'] In [229]: subjects[0] Out[229]: 'KAA' In [230]: subjects[0] == 'KAA' Out[230]: True In [231]: np.where(subjects == 'KAA') Out[231]: () In [232]: pylab.find(subjects == 'KAA') Out[232]: array([], dtype=int32) It doesn't seem to matter if I make the list into an array: In [233]: np.array(subjects) Out[233]: array(['KAA', 'CCS', 'EJS', 'MNM', 'JHS', 'LJL', 'DVA', 'FCL', 'CNC', 'KFM', 'APM', 'GMC'], dtype='|S3') In [234]: pylab.find(subjects == 'KAA') Out[234]: array([], dtype=int32) In [235]: np.where(subjects == 'KAA') Out[235]: () What am I doing wrong? What does it mean that the dtype is IS3?
I think you made a typo. Try changing line 233 to subjects = np.array(subjects)
type(subjects) <type 'list'> np.where(subjects == 'KAA') () np.where(np.asarray(subjects) == 'KAA') (array([0]),)
Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Ariel Rokem
-
Keith Goodman
-
Robert Kern