Question says it all basically.
I'm trying to find words that contain specific letters in order. So if I search for "ers" it should only give out words like "ERStaunding", etc.
That's what I have so far: (doesn't work because a string requires a string as left operand) but you get the idea:
sorted = []
for i in range(0,len(words)): #"words" is a list of 1600 words
if possible in words[i]: #error because of what I said before
sorted.append(words[i]) #"possible" is a string of letters
print(sorted)
thanks in advance!
edit:
This is what I put in/what comes out: (in pseudocode, it's hangman for uni)
user input: "hello" - word to be guessed
cpu guesses: "h" (for example) "is h part of your word? (y/n)"
user input: "y"
computer knows that "h" is at position 1 (by comparing to the word that has to be guessed) - stupid, I know but this is an example :D : h _ _ _ _
now the pc should compare what he has (h up to this point at index 0) to words in a database (in my case words.txt) and only leave the ones that have "h" as their first letter. This process should be interchangeable. For example: if the pc has _ _ l l _, he should still be able to filter the words.
I hope that's what you meant with an example, hope this helps :D
CodePudding user response:
This gives only words that contain possible
:
words = ["test", "list", "here"] #list of words
possible = "st" #string to find within each word
sortedList = [] #output list defined
for word in words: #for each word
if possible.lower() in word.lower(): #if the word includes the string
sortedList.append(word) #add the word to the output
print(sortedList) #print output
Output:
['test', 'list']
CodePudding user response:
I think you just want to check whether the given word is present at the begining.
substring=substring.lower()
for i in range(0,len(words)): #"words" is a list of 1600 words
s=words[i].lower()
if substring in s: # word found at the begining
sorted.append(words[i])