I’m trying to filter a list, I can get exact matches, but I’m having trouble getting partial matches. Indices 0 & 9 are true, because they are exact, but 1 & 10 have extra stuff after it, which is closer to my real-world example. You can see some of the different versions I’ve tried are still in the code block.
import sys
import difflib
from difflib import SequenceMatcher
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
string_list = IN[0]
required_string = IN[1]
result =[]
for s in string_list:
similar_ratio = similar(s,required_string)
if similar_ratio > 0.8: # 80 % similar
result.append(s)
else:
pass
OUT = result
All depends on what match you’re looking for, and what edge cases you might expect. E.g. the code in @Ewan_Opie’s post will catch “Relevation!!” which you might not want, but miss “Internal Elevation” which you probably do. However, it can pick up typos like “Elvation” which Regex wouldn’t.