I have a list of lists (many tokenised sentences). For anyone who doesn't know what tokenised sentences are, my list is like so:
list1 = [['hello', 'my', 'name'], ['this', 'is', 'stack', 'exchange'], ... ]
I have a list of key words as well, key_words
.
For every sentence in list
, I want to check if it is in key_words
. Moreover, I want a single method to be applied to each sentence. Below is my working (but inefficient) code:
list1 = [['hello', 'my', 'name'], ['this', 'is', 'stack', 'exchange']]
key_words = ['hello', 'name', 'stack']
def get_features(sentence, key_words):
return [word for word in sentence if word in key_words]
f = []
for sent in list1:
f.append(get_features(sent, key_words))
This is fine, but my dimensions are like so:
len(list1) = 45,000
len(key_words) = 35,000
This is of course inefficient, and I would like to find a faster way of doing this. Could dictionaries be utilised in some way? I was thinking of changing key_words
from a list to a dictionary of key:value = word:1. Then I could do something like
return [word for word in sentence if key_words[word] does not give error]
but I'm unsure how if does not give error
would be implemented. Doing this would allow O(1) access to words in key_words if they are actually in there, rather than having to search the whole list until it is found, with O(n).
key_words = set(['hello', 'name', 'stack'])
and see if it's fast enough. Also you shouldn't name variableslist
, as it is a type name. \$\endgroup\$set
s? \$\endgroup\$