我有一个包含字典中所有单词的csv,并且我想有一个函数,它给定三个字符,按照给定的顺序从csv中搜索包含给定字符的所有单词。
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发布于 2021-07-11 00:17:06
使用正则表达式和列表理解:
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)获取words_with_a_b_c
['hello']https://stackoverflow.com/questions/68329183
复制相似问题