I have one word "MINORITY", and I'd like to know if there is a simple way to check if the word "MINORITY" is in a list of words. However, the trick part, is in the list where some words might be a subset of the word i'm looking up. My list is the following:
word = 'MINORITY'
list_words = ['HELLO','STACK','OVER','MINORIT','FLOW']
In this case I'd like to get the index 3, as 'MINORIT' is a subset of 'MINORITY'
Note: I'd like to not iterate through the list of words, but use instead a function such as 'isin()'
This is a pretty simple one to solve, you can use a library called difflib
to search for similar words within the list.
word = 'MINORITY'
list_words = ['HELLO','STACK','OVER','MINORIT','FLOW']
import difflib
outcome = difflib.get_close_matches(word, list_words)
print(outcome)
If you are looking to get the index of the outcomes, you can do list_words.index(outcome)
def check_member(word, lst):
" finds which index of lst is a substring of word (if any) "
return (i for i, x in enumerate(lst) if x in word)
Test
list_words = ['HELLO','STACK','OVER','MINORIT','FLOW']
for word in ['foo', 'MINORITY', 'minority', 'HELLO123', 'STACK', "12FLOW", 'bob']:
i = next(check_member(word, list_words), None)
if i:
print(f' {word} matches {list_words[i]} at index {i}')
else:
print(f'{word} - no matches')
Output
foo - no matches
MINORITY matches MINORIT at index 3
minority - no matches
HELLO123 - no matches
STACK matches STACK at index 1
12FLOW matches FLOW at index 4
bob - no matches
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.