+ 1

Python - Delete words in .txt file that repeat letters

Soooooo, I'm going to apologize up front because I'm probably going to make this more confusing than I need to. I want to: - user input usable letters ✅ - import a .txt file of words into a list ✅ - remove all words less than 4 letters long ✅ - generate list of all words that the letters can make ✅ BUT now I want to only recognize the EXACT letters available. EXAMPLE: If the given letters are "h", "a", "p", "y" then "happy" cannot be an available word because only one "p" was given, not 2? Thanks for any help 👍👍 https://code.sololearn.com/cc8c1OmGBbol/?ref=app

29th May 2019, 4:00 AM
BelowZer0
BelowZer0 - avatar
8 ответов
+ 3
from collections import Counter with open("/storage/emulated/0/DCIM/_Python/words_alpha.txt") as f: words = f.read().split() letters = input().lower() [print(word)for word in words if Counter(word)==Counter(letters)]
29th May 2019, 9:10 PM
Diego
Diego - avatar
+ 6
My bad. Try words = list(map(str.lower, f.read().split())) (second line)
29th May 2019, 12:59 PM
Anna
Anna - avatar
+ 4
with open("/storage/emulated/0/DCIM/_Python/words_alpha.txt") as f: words = map(str.lower, f.read().split()) letters = input("Enter Possible letters: ").lower() def possibleWords(text_file,character): print([word for word in text_file if len(word) > 3 and Counter(word) == Counter(character)])
29th May 2019, 5:58 AM
Anna
Anna - avatar
+ 2
BelowZer0 Changed "Letters" to "letters" and "Chatacter_Set" to "characters". def possibleWords(text_file, characters): for word in text_file: if Counter(word) == characters: print(word) possibleWords(words, Counter(letters)) EDIT: Fixed it. If that doesn't work try changing "split()" for "split(r'\n')".
29th May 2019, 4:42 AM
Diego
Diego - avatar
+ 2
Also Diego and Anna, huge thank you for the responses, it was very appreciated!
29th May 2019, 8:34 PM
BelowZer0
BelowZer0 - avatar
+ 1
Diego sorry, uploaded the program from before fixing "replace all" errors. But with your changes I'm getting no return? Any other suggestions?
29th May 2019, 5:35 AM
BelowZer0
BelowZer0 - avatar
+ 1
Anna sorry if I missed something, when i made the changes I'm getting a return of an empty list? Any chance you know what I messed up? And I apologize im working off my phone and that may be causing some of the issues? Thank you so much for the help!
29th May 2019, 12:21 PM
BelowZer0
BelowZer0 - avatar
+ 1
**Update**: After fighting this for like 10 hours and going down rabbit hole after rabbit hole, I had a freaking epiphany and realised there is a way easier way to do this and about half the amount of code, so here it is 🤦‍♂️ ___________________________ import re from collections import Counter with open("/storage/emulated/0/DCIM/_Python/words_alpha.txt") as f: words = f.read() shortword = re.compile(r'\W*\b\w{1,3}\b') words=shortword.sub('', words).split() letters = input("Enter Possible letters: ").lower() for word in words: if not Counter(word) - Counter(letters): print(word)
29th May 2019, 8:23 PM
BelowZer0
BelowZer0 - avatar