Issue
So I have a numpy array containing lists (of various lengths) which consist of strings. I'd like to find all rows where a certain string appears in, e.g. I want to find all numpy array indices for which the corresponding list contains the string 'hello'. I thought I could simply use
np.where('hello' in np_array)
but unfortunately this just results in an empty numpy array. Any ideas?
Solution
import numpy as np
np_arr = np.array([['hello', 'salam', 'bonjour'], ['a', 'b', 'c'], ['hello']])
vec_func = np.vectorize(lambda x: 'hello' in x)
ind = vec_func(np_arr)
Output:
#Ind:
array([ True, False, True])
# np_arr[ind]:
array([list(['hello', 'salam', 'bonjour']), list(['hello'])], dtype=object)
However, if you wish to get the output as a list of integers for indices, you might use:
np.where(vec_func(np_arr))
#(array([0, 2], dtype=int64),)
Answered By - aminrd
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.