I have an array to which indices are looked up
arr_to_lookup = np.array(['cat', 'dog', 'cow', 'house', 'fish'])
And then I have a list of words which indices I want to get if they are present in arr_to_lookup
words = np.array(['cat', 'house', 'big plane', cat])
Expected result
word_indices = [0, 3, 0] # 'big plane' does not exist, therefore it is ignored
My current solution is to create index dictionary and do a lookup that way, however, it is quite time consuming as arr_to_lookup is a big array
arr_to_lookup_dict = {word: i for i, word in enumerate(arr_to_lookup)}
indices = []
for word in words:
try:
indices.append(arr_to_lookup_dict[word])
except KeyError:
continue
Is it worth looking into Cython for this problem?