I'm working on a script that supposed to find words that contain given letters by user has input. The script works however not exactly in a way I want. For example if I input letters:stop The scripts prints out words I have input but are repeated such like: stop, stoper, stereotype. There shouldn't be the word: "Stereotype" because I've input only only one: "e" and "t
letters = input('letters: ')
words = open('newwords').read().splitlines()
#print (words)
print(".......................")
for word in words:
if all(x in word for x in letters):
for x in letters:
if word.count(x) == letters.count(x):
print(word)
I don't know how your 'newwords' look like. I try something like this:
stop
stoper
stereotype
the code is same as yours except words = open('test.txt').read().splitlines()
letters: stop
.......................
stop
stop
stop
stop
stoper
stoper
stoper
stoper
stereotype
stereotype
stereotype
Your for x in letters
check one letter at a time. As there is same count for "s", "o" and "p", stereotype is printed three times (not four because of different count for "t") .
You may change your last part to this so the word only printed out after all letter count is checked.
for word in words:
if all(word.count(x) == letters.count(x) for x in letters):
print(word)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.