Reputation: 1
I have a csv with all the words from the dictionary, and i want to have a function that given three characters, searches for all the words from the csv that contain the given characters in the given order.
def read_words(registro):
with open(file, encoding="utf-8") as f:
lector = csv.reader(f)
palabras = [p for p in lector]
return palabras
file= ("Diccionario.csv")
register = read_words(file)
def search_for_words_with(register, a, b, c):
res = []
for w in register:
if a in w:
if b in w:
if c in w:
res.append(w)
return res
Upvotes: 0
Views: 176
Reputation: 1169
Use regex and list comprehension:
import regex as re
def search_for_words_with(register, a, b, c):
words_with_a_b_c = [w for w in register if re.search(a + '.*' + b + '.*' + c, w)]
return words_with_a_b_c
register = ['hello', 'worldee']
a, b, c = 'e', 'l', 'o'
words_with_a_b_c = search_for_words_with(register, a, b, c)
to get words_with_a_b_c
['hello']
Upvotes: 1