[Numpy-discussion] Test if one element of string array is in a defined list

Eric Emsellem eemselle at eso.org
Mon Mar 22 05:02:00 EDT 2010


I would like to test whether strings in a numpy S array are in a given list but 
I don't manage to do so. Any hint is welcome.

# So here is an example of what I would like to do
# I have a String numpy array:

import numpy as num
Sarray = num.asarray(["test1","test2","tutu","toto"])
Farray = num.arange(len(Sarray))
mylist = ["tutu","hello","why"]

# and I would like to do:

result = num.where(Sarray in mylist, Farray, 0)

===> but of course I get a ValueError since the first test syntax is wrong. I 
would like to be able to do:

            Sarray in mylist

and get the output:

            array([False, False, True, False],dtype=bool)

since only the 3rd string "tutu" of Sarray is in mylist.

Any input is welcome.


More information about the NumPy-Discussion mailing list