python - Finding words in a file in which all characters are contained in another word -


so i'm doing http://wiki.openhatch.org/scrabble_challenge#step_3:_find_valid_words , i've gotten stuck 1 part. i'm trying use nested loops go through every word in word list (a text file have open), , every letter in word, see if letter contained in user input. code far is

import argparse import sys  file=open('sowpods.txt','r')#opens file of words used in scrabble scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,          "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,          "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,          "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,          "x": 8, "z": 10}#dictionary letter values parser=argparse.argumentparser(description='designed in scrabble') #command line interface using argparse module parser.add_argument("word",help="input letters",nargs='?')#adds  positional argument description args=parser.parse_args()  if args.word none:     print('error need letters work')     sys.exit() else:     print(str.lower(args.word))#converts the input argument word  lowercase letters letter in file:     i=str.lower(letter)     in args.word:         valid=[]         valid.append(i)         print(valid) 

the expected output should this

python scrabble.by zzaaeei size ziz 

basically list of valid words.

once have list can value of word using scores dictionary i'm stuck since thought nested loops best way. help.

the file sowpods.txt contains list of words brief snippet

aa aah aahed aahing aahs aal aalii aaliis aals aardvark aardvarks aardwolf aardwolves aargh aarrgh aarrghh aarti aartis aas aasvogel aasvogels ab aba abac abaca abacas abaci aback abacs abacterial abactinal abactinally small snippet it's pretty file of valid scrabble words 

just use set difference operation:

word = args.word.lower() valid = [] line in file:     line = line.lower().strip()     if not (set(line) - set(word)):         valid.append(i)  print(valid) 

if line has characters contained in word, set difference between these 2 empty set, not <empty set> true.

i'm hoping each word in file contained on separate line, otherwise neither work nor efficient if did.

example:

⇒ word = zzaaeei   line = zeze  ⇒ set_word = {'z', 'a', 'e', 'i'}   set_line = {'z', 'e' }  ⇒ set_line - set_word = {'z', 'e' } - {'z', 'a', 'e', 'i'}   ⇒ set() 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -