在单词游戏中查找单词

时间:2014-04-22 20:53:54

标签: python for-loop words

我正在尝试编写一个程序,允许用户输入一个单词,然后找到隐藏在该单词文本文件中的单词中长度为4或更大的所有单词。到目前为止,我的代码可以检测到用户输入的单词中没有混淆的单词。例如,如果我输入“房屋”,输出将显示房屋,房屋,房屋,房屋,使用,使用等。它还应该识别软管,软管,鞋子,鞋子,色调等等。

我知道itertools是最简单的解决方案,但我想使用仅使用循环,字典和列表的不同方法。

到目前为止,这是我的代码:

def main():
    filename = open('dictionary.txt').readlines()
    word_list = []
    for line in filename:
        word_list.append(line.strip())

    print 'Lets Play Words within a Word!\n'
    word = raw_input('Enter a word: ')
    words_left = 0
    for words in word_list:
        letters = list(words)
        if words in word:
            print words
            words_left += 1
        else:
            False

我尝试创建的输出格式应如下所示:

Lets play Words within a Word!

Enter a word: exams

exams ---  6 words are remaining
> same #user types in guess
Found!  # prints 'Found!' if above word is found in the dictionary.txt file

exams ---  5 words are remaining
> exam
Found!

exams ---  4 words are remaining
> mesa
Found!

exams ---  3 words are remaining
> quit() #if they type this command in the game will end

我还想添加一个游戏摘要,根据Scrabble的信件评分来跟踪用户的得分,但这会在以后发生。

4 个答案:

答案 0 :(得分:2)

可以真正帮助你的是itertools.permutations。这个方便的函数接受一个序列(如字符串),并为您提供指定长度的所有排列。

以下是我建议您使用它的方法:

import itertools

def main():
    with open('dictionary.txt') as file:
        word_list = set(line.strip() for line in file) # Use a set instead of a list for faster lookups

    print 'Lets Play Words within a Word!\n'
    word = raw_input('Enter a word: ')

    subwords = set() # In case a word can be made in multiple ways
    for i in range(4, len(word)+1):
        for permutation in itertools.permutations(word, i):
            word_to_check = ''.join(permutation)
            if word_to_check in word_list:
                subwords.add(word_to_check)

这将检查单词的所有可能排列,以查看哪些单词是单词,并保留单词中找到的所有单词的集合(无重复)。然后,当用户猜测时你可以检查

if user_guess in subwords and user_guess not in already_found_words:

答案 1 :(得分:0)

似乎是itertools

的工作
import itertools

word = "house"

arrangements = []

# get the combinations
for i in range(len(word)+1):
    arrangements += list(itertools.permutations(word,i))

# turn the tuples into strings
for i in range(len(arrangements)):
    arrangements[i] = ''.join(arrangements[i])

print(arrangements)

看哪,安排是一个巨大的清单,列出了“房子”这个词的每一个可能的排列。随意过滤它寻找真实的单词。

输出:

['', 'h', 'o', 'u', 's', 'e', 'ho', 'hu', 'hs', 'he', 'oh', 'ou', 'os', 'oe', 'uh', 'uo', 'us', 'ue', 'sh', 'so', 'su', 'se', 'eh', 'eo', 'eu', 'es', 'hou', 'hos', 'hoe', 'huo', 'hus', 'hue', 'hso', 'hsu', 'hse', 'heo', 'heu', 'hes', 'ohu', 'ohs', 'ohe', 'ouh', 'ous', 'oue', 'osh', 'osu', 'ose', 'oeh', 'oeu', 'oes', 'uho', 'uhs', 'uhe', 'uoh', 'uos', 'uoe', 'ush', 'uso', 'use', 'ueh', 'ueo', 'ues', 'sho', 'shu', 'she', 'soh', 'sou', 'soe', 'suh', 'suo', 'sue', 'seh', 'seo', 'seu', 'eho', 'ehu', 'ehs', 'eoh', 'eou', 'eos', 'euh', 'euo', 'eus', 'esh', 'eso', 'esu', 'hous', 'houe', 'hosu', 'hose', 'hoeu', 'hoes', 'huos', 'huoe', 'huso', 'huse', 'hueo', 'hues', 'hsou', 'hsoe', 'hsuo', 'hsue', 'hseo', 'hseu', 'heou', 'heos', 'heuo', 'heus', 'heso', 'hesu', 'ohus', 'ohue', 'ohsu', 'ohse', 'oheu', 'ohes', 'ouhs', 'ouhe', 'oush', 'ouse', 'oueh', 'oues', 'oshu', 'oshe', 'osuh', 'osue', 'oseh', 'oseu', 'oehu', 'oehs', 'oeuh', 'oeus', 'oesh', 'oesu', 'uhos', 'uhoe', 'uhso', 'uhse', 'uheo', 'uhes', 'uohs', 'uohe', 'uosh', 'uose', 'uoeh', 'uoes', 'usho', 'ushe', 'usoh', 'usoe', 'useh', 'useo', 'ueho', 'uehs', 'ueoh', 'ueos', 'uesh', 'ueso', 'shou', 'shoe', 'shuo', 'shue', 'sheo', 'sheu', 'sohu', 'sohe', 'souh', 'soue', 'soeh', 'soeu', 'suho', 'suhe', 'suoh', 'suoe', 'sueh', 'sueo', 'seho', 'sehu', 'seoh', 'seou', 'seuh', 'seuo', 'ehou', 'ehos', 'ehuo', 'ehus', 'ehso', 'ehsu', 'eohu', 'eohs', 'eouh', 'eous', 'eosh', 'eosu', 'euho', 'euhs', 'euoh', 'euos', 'eush', 'euso', 'esho', 'eshu', 'esoh', 'esou', 'esuh', 'esuo', 'house', 'houes', 'hosue', 'hoseu', 'hoeus', 'hoesu', 'huose', 'huoes', 'husoe', 'huseo', 'hueos', 'hueso', 'hsoue', 'hsoeu', 'hsuoe', 'hsueo', 'hseou', 'hseuo', 'heous', 'heosu', 'heuos', 'heuso', 'hesou', 'hesuo', 'ohuse', 'ohues', 'ohsue', 'ohseu', 'oheus', 'ohesu', 'ouhse', 'ouhes', 'oushe', 'ouseh', 'ouehs', 'ouesh', 'oshue', 'osheu', 'osuhe', 'osueh', 'osehu', 'oseuh', 'oehus', 'oehsu', 'oeuhs', 'oeush', 'oeshu', 'oesuh', 'uhose', 'uhoes', 'uhsoe', 'uhseo', 'uheos', 'uheso', 'uohse', 'uohes', 'uoshe', 'uoseh', 'uoehs', 'uoesh', 'ushoe', 'usheo', 'usohe', 'usoeh', 'useho', 'useoh', 'uehos', 'uehso', 'ueohs', 'ueosh', 'uesho', 'uesoh', 'shoue', 'shoeu', 'shuoe', 'shueo', 'sheou', 'sheuo', 'sohue', 'soheu', 'souhe', 'soueh', 'soehu', 'soeuh', 'suhoe', 'suheo', 'suohe', 'suoeh', 'sueho', 'sueoh', 'sehou', 'sehuo', 'seohu', 'seouh', 'seuho', 'seuoh', 'ehous', 'ehosu', 'ehuos', 'ehuso', 'ehsou', 'ehsuo', 'eohus', 'eohsu', 'eouhs', 'eoush', 'eoshu', 'eosuh', 'euhos', 'euhso', 'euohs', 'euosh', 'eusho', 'eusoh', 'eshou', 'eshuo', 'esohu', 'esouh', 'esuho', 'esuoh']

答案 2 :(得分:0)

您可以使用permutations()模块中的itertoolsset来删除重复的字符串。

>>> from itertools import permutations
>>> set((''.join(p) for i in range(1, len('exams') +1) for p in permutations('exams', i)))

这会给你

set(['eaxs', 'xea', 'xmsea', 'emsax', 'xem', 'seam', 'mxse', 'amsxe', 'msxe', 'esma', 'mxsa', 'xsma', 'sexma', 'samex', 'xsme', 'xes', 'maxse', 'mexas', 'eaxm', 'mexsa', 'easm', 'axems', 'asmex', 'sxaem', 'xme', 'emasx', 'exsa', 'mxeas', 'xma', 'amxse', 'amexs', 'asxm', 'xaems', 'xsem', 'xmesa', 'easx', 'sxame', 'xsea', 'maesx', 'xms', 'eamsx', 'exsm', 'exmsa', 'axse', 'axmes', 'sxme', 'ame', 'mxs', 'sxema', 'axsm', 'amx', 'amesx', 'xemsa', 'measx', 'smea', 'xesma', 'axmse', 'ams', 'mxe', 'esm', 'mxa', 'seamx', 'aes', 'mxas', 'ema', 'meax', 'aex', 'mxea', 'xmae', 'mexs', 'sema', 'mesxa', 'sxeam', 'saemx', 'xsa', 'msax', 'mxae', 'ems', 'xse', 'x', 'aexms', 'sxem', 'aem', 'emx', 'xsm', 'em', 'mxsae', 'emas', 'axms', 'samxe', 'msea', 'sxmae', 'emax', 'esmx', 'ea', 'emxs', 'sex', 'emaxs', 'mesx', 'ex', 'sea', 'xase', 'exsma', 'sem', 'xmea', 'esmxa', 'es', 'emxa', 'smx', 'axes', 'semx', 'smaxe', 'esx', 'xaesm', 'asxme', 'axem', 'esa', 'aexs', 'sme', 'esxma', 'maex', 'sma', 'sexa', 'smeax', 'xemas', 'mea', 'exam', 'sxmea', 'semxa', 'asx', 'sexm', 'aexsm', 'xsmae', 'amex', 'esamx', 'emxsa', 'mes', 'aesxm', 'smax', 'saxm', 'ames', 'exas', 'mex', 'asm', 'xmsae', 'esxam', 'emsx', 'saxe', 'xems', 'eaxsm', 'xsema', 'xmas', 'saexm', 'meaxs', 's', 'exams', 'esmax', 'emsa', 'xema', 'xames', 'aemsx', 'amxs', 'aesmx', 'ase', 'mesax', 'xm', 'aesm', 'xa', 'asemx', 'msaxe', 'xe', 'xasm', 'seax', 'mexa', 'smae', 'amxe', 'xs', 'asexm', 'aesx', 'esax', 'masx', 'msx', 'xmaes', 'msxea', 'amse', 'xas', 'xsame', 'msxae', 'xsae', 'maes', 'emsxa', 'xam', 'esam', 'xmes', 'xsam', 'mse', 'xae', 'msa', 'exasm', 'aemx', 'smxea', 'xame', 'eam', 'axsem', 'mase', 'aems', 'amsx', 'xaes', 'smxe', 'eax', 'xaem', 'msexa', 'xesm', 'asmxe', 'asxe', 'masex', 'xesa', 'eas', 'amxes', 'esaxm', 'mxaes', 'me', 'esxa', 'mseax', 'ma', 'sxae', 'emxas', 'esxm', 'sxam', 'eamxs', 'msxa', 'ms', 'amsex', 'xasme', 'aemxs', 'xesam', 'mx', 'xmase', 'axme', 'sxa', 'msaex', 'xmeas', 'ae', 'sxe', 'sam', 'maxes', 'maexs', 'mxsea', 'xeasm', 'am', 'sxm', 'sae', 'easmx', 'xasem', 'as', 'sax', 'meas', 'msex', 'xsmea', 'ax', 'asxem', 'sxea', 'sexam', 'mxase', 'xseam', 'exsam', 'saex', 'axesm', 'saem', 'maxs', 'masxe', 'asmx', 'same', 'smexa', 'samx', 'saxem', 'asme', 'saxme', 'xsaem', 'exmas', 'msae', 'maxe', 'seaxm', 'axsme', 'mas', 'asex', 'xams', 'eaxms', 'max', 'xeams', 'exms', 'smxae', 'xmsa', 'smaex', 'mae', 'xmse', 'asem', 'aexm', 'smxa', 'mesa', 'sxma', 'exma', 'mxes', 'a', 'axs', 'eams', 'sx', 'e', 'xeas', 'mxesa', 'exa', 'semax', 'eamx', 'exm', 'm', 'easxm', 'xamse', 'exs', 'sm', 'axe', 'smex', 'sa', 'xeam', 'se', 'axm'])

请注意,使用较大的字符串可能效率很低!

答案 3 :(得分:0)

使用itertools为您提供帮助:

from itertools import permutations
print '\nLets play Words within a Word!\n'
filename = raw_input('Enter the path to the file: ')
wordlist = open('/usr/share/dict/words', 'r')
wordlist = [word for word in wordlist.read().split() if len(word) >= 4]
word = raw_input('Enter a word: ')
possible_words = list(set([words for words in [''.join(p) for i in range(1, len(word)+1) for p in permutations(word, i)] if len(words) >= 4 and words in wordlist]))
guessed = []
while len(guessed) < len(possible_words):
        left = len(possible_words)-len(guessed)
        print 'The number of words left to guess is %s!' %(str(left))
        guess = raw_input('\nEnter your guess: ')
        if guess in possible_words:
                print 'Correct!\n'
                guessed.append(guess)
        else:
                if guess == '':
                        for k in guess:
                                print k,
                else:
                        print 'Incorrect!\n'

print 'You won!'

运行如下:

bash-3.2$ python everyword.py

Lets play Words within a Word!

Enter the path to the file: /usr/share/dict/words
Enter a word: hello
The number of words left to guess is 4!

Enter your guess: incorrect
Incorrect!

The number of words left to guess is 4!

Enter your guess: hole 
Correct!

The number of words left to guess is 3!

Enter your guess: hello
Correct!

The number of words left to guess is 2!

Enter your guess: holl
Correct!

The number of words left to guess is 1!

Enter your guess: blah
Incorrect!

The number of words left to guess is 1!

Enter your guess: hell
Correct!

You won!
bash-3.2$