Answer the question
In order to leave comments, you need to log in
Which algorithm for finding substrings in a list of tuples will be faster than comprehension?
Given: A list of about 100k tuples. Each tuple has two elements, the first is an id and the second is a string. It is necessary to select all id from those tuples where the strings contain a certain substring.
tuples = [(id1, 'cheese trees'), (id2, 'freezy breeze'),...]
vals = ['cheese', 'flees']
ids = {i[0] for i in tuples if any(val in i[1] for val in vals)}
output: {id1}
Answer the question
In order to leave comments, you need to log in
Aho-Korasik
algorithm .
Put all your keys into the burr, as described in the link. Then for each tuple, search in the i[1] line, if found, then add your id to the answer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question