-2

I have a list with types of cheese and I want to be able to search for gouda by just writing "g" and "o" instead of writing the full sentence.

I've looked for solutions but none are exactly what I am looking for. Maybe this is something common but I just started a week ago with Python I don't know many of the terms. For some reason I got this cancelled so Im writing this paragraph so the person that answered can answer again

1 Answers1

0

Here is a link to another StackOverflow post I found: Link. This explains what I think you are looking for in your problem.

This code will print gouda from the wordlist:

wordlist = ['gouda','miss','lake','que','mess']

letters = set('g')

for word in wordlist:
    if letters & set(word):
        print(word)

All you have to do is set whatever letters you want to search for in the list to the letter variable (in the brackets) and it will return the words that contain the letters you entered.

ex. I added gouda (your example) to this list. If you set the letters variable, to g, it searches the wordlist for any words that contain the letter g, in this case it will return gouda from the wordlist as it is the only word that contains the letter 'g'.

The only downfall of this is if you enter 'ms' to search this wordlist you will get two responses, miss and mess as they both contain letters 'm,s' so in some cases you will have to be more specific if you only want one word to be returned.

Note: this is not my code, I got it from the post linked here, and above.

Jack
  • 44
  • 6
  • Really thankful for your answer, but I want to know if I can for example, specify that im looking for a word that starts with go, because, for example, if I have in the same list "geno" or something that contains those letters but not in the beginning, it ends up being a mess and I have a lot of words in the output(to sum up I want to know how I can specify that Im looking for go in the first two letters of a string in a list and exclude anything that doesnt start with that(like my "geno" example)) – Diego Subiabre Aug 18 '20 at 01:32
  • Thanks for trying tho, if I find a solution I will definitely post it here. – Diego Subiabre Aug 19 '20 at 13:34