0

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
6
  • Please show your work, what you have tried. I'm sorry, but SO is not a software writing service. Commented Jul 10, 2021 at 15:44
  • I uploaded what i have for now, srry in quite new on this. Commented Jul 10, 2021 at 15:53
  • Much better, thanks. Now, can you also indicate in what way it's not working, or any errors you are getting? Commented Jul 10, 2021 at 15:55
  • And ideally make the example a minimum reproducible example which we can cut and paste and run to see the problem on our own computers. If the problem is with search_for_words_with, for example, you could give us a hard-coded register array and a call to the function that illustrates the problem. Commented Jul 10, 2021 at 15:57
  • Use a regexp: firstchar.*secondchar.*thirdchar Commented Jul 10, 2021 at 16:07

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.