[Numpy-discussion] Usage of numpy.where and pylab.find with strings

Robert Kern robert.kern at gmail.com
Mon Jan 26 15:00:07 EST 2009


On Mon, Jan 26, 2009 at 13:48, Ariel Rokem <arokem at 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



More information about the NumPy-Discussion mailing list