I'm doing NLP in Python 3 and trying to optimize the speed of a code. The code converts a list of words to a list (or array) of numbers using a given dictionary.
For example,
mydict = {'hello': 0, 'world': 1, 'this': 2, 'is': 3, 'an': 4, 'example': 5}
word_list = ['hello', 'world']
def f(mydict, word_list):
return [mydict[w] for w in word_list]
# f(mydict, word_list) == [1, 2]
I want to speed up the function f, especially when the word_list is about 100 words long. Is it possible? Use of external libraries like nltk, spacy, numpy etc is OK.
Currently, it takes 6us on my laptop.
>>> %timeit f(mydict, word_list*50)
6.74 us +- 2.77 us per loop (mean +- std. dev. of 7 runs, 100000 loops each)