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

Keith Goodman kwgoodman at gmail.com
Mon Jan 26 15:00:29 EST 2009


On Mon, Jan 26, 2009 at 11:48 AM, 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)
> 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]),)



More information about the NumPy-Discussion mailing list