Python在用户输入文字游戏中查找单词

时间:2014-04-28 01:00:56

标签: python list loops dictionary

我正在尝试编写一个程序,允许用户输入一个单词,然后找到隐藏在该单词文本文件中的单词中长度为4或更大的所有单词。到目前为止,我的代码可以检测到用户输入的单词中没有混淆的单词。例如,如果我输入houses,则输出将显示househouseshoususe,{{1} }。它还应该识别useshosehosesshoeshoes等。

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

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

hues

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

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

所以我的问题是,在输入基本单词后(在ex ex的考试中),如何确定该单词中的单词总数以及用户输入的单词猜测是否在文本文件中?如果找到单词也打印。

3 个答案:

答案 0 :(得分:0)

一个天真的实现(使用collections.Counter):

>>> all_words = ['foo', 'bar', 'baz', 'hose', 'hoses', 'shoe', 'shoes', 'hues', 'house', 'houses', 'ho', 'us', 'use', 'uses', 'shoe', 'same', 'exam', 'mesa', 'mass']

>>> def find_hidden(user_input):
        from collections import Counter
        user_word_counts = Counter(user_input)
        for word in all_words:
            isvalid = True
            for letter, count in Counter(word).iteritems():
                if user_word_counts[letter] == 0 or user_word_counts[letter] < count:
                    isvalid = False
                    break
            if isvalid: yield word


>>> list(find_hidden("houses"))
['hose', 'hoses', 'shoe', 'shoes', 'hues', 'house', 'houses', 'ho', 'us', 'use', 'uses', 'shoe']
>>> list(find_hidden("exams"))
['same', 'exam', 'mesa']

或者,

使用排列:

>>> all_words = ['foo', 'bar', 'baz', 'hose', 'hoses', 'shoe', 'shoes', 'hues', 'house', 'houses', 'ho', 'us', 'use', 'uses', 'shoe', 'same', 'exam', 'mesa', 'mass']
>>> def permutations(s, n): # could use itertools.permutations
        if n == 1:
            for c in s:
                yield c
        for i in range(len(s)):
            for p in permutation(s[:i]+s[i+1:], n-1):
                yield s[i] + p


>>> def find_hidden(input_str):
        for word_len in range(2, len(input_str)+1):
            for word in permutations(input_str, word_len):
                if word in all_words:
                    yield word


>>> set(find_hidden("houses"))
set(['use', 'hose', 'shoes', 'houses', 'house', 'us', 'hues', 'hoses', 'uses', 'ho', 'shoe'])
>>> set(find_hidden("exams"))
set(['mesa', 'exam', 'same'])

答案 1 :(得分:0)

这有效:

# read from file in actual implementation
all_words = [
    "foo", "bar", "baz", "hose", "hoses", "shoe", "shoes", "hues", "house",
    "houses", "ho", "us", "use", "uses", "shoe", "same", "exam", "mesa", "mass"]

RETAIN_ORDERING = False

def matches(inp, word):
    if inp[0] == word[0]:
        return (
            True if len(word) == 1 else
            False if len(inp) == 1 else
            matches(inp[1:], word[1:]))
    else:
        return matches(inp[1:], word) if len(inp) >= 2 else False

# with sorting enabled, "houses" will also match "shoe"; otherwise not
def maybe_sort(x):
    return x if RETAIN_ORDERING else ''.join(sorted(x))

inp = raw_input("enter a word: ")

results = [word for word in all_words if matches(maybe_sort(inp), maybe_sort(word))]

print results

<强>输出:

$ python matches.py 
enter a word: houses
['hose', 'hoses', 'shoe', 'shoes', 'hues', 'house', 'houses', 'ho', 'us', 'use', 'uses', 'shoe']
$ python matches.py 
enter a word: exams
['same', 'exam', 'mesa']

如果你想避免像shoe那样字母排序与输入不一样的匹配,只需设置RETAIN_ORDERING = True

答案 2 :(得分:0)

这样的事情应该有用......

wordlist=[list of words]
solutionlist=[]
userword=[userword[i] for i in range(len(userword))]
for word in wordlist:
    inword=True
    letters=[word[j] for j in range(len(word))]
    for letter in set(letters):
        if letters.count(letter)>userword.count(letter):
            inword=False
            break
    if inword:
        solutionlist.append(word)

for line in solutionlist:
    print line
相关问题