2012-12-06 26 views
0
def anagram(word,check): 
    for letter in word: 
     if letter in check: 
      check = check.replace(letter, '') 
     else: 
      return 0 
    return 1 


while True: 
    f = open('dictionary.txt', 'r') 
    try: 
     user_input = input('Word? ') 
     for word in f: 
      word = word.strip() 
      if len(word)==len(user_input): 
       if word == user_input: 
        continue 
       elif anagram(word, input): 
        print (word) 
        #try: 
         #if word == 1: 
          #print ('The only anagram for', user_input, 'is', word) 
         #elif word > 1: 
          #print ('The anagrams for', user_input, 'are', word) 
        #except TypeError: 
         #pass 
    except EOFError: 
     break 
    f.close() 

该函数按我希望的方式工作,但我需要一些输出帮助。我希望输出结果在一行中,措辞应该反映找到的字符的数量。 (即'只有一个字谜','anagrams','没有anagrams',或'这个词不在字典中')代码中的注释是我尝试过的。谢谢你的帮助。Python 3.3:Anagram函数的输出

+1

您应该在'anagram()'中返回'True'和'False'。 –

+0

这不会解决您的问题,但我建议将'if len(word)== len(user_input)'和'if word == user_input'行移入'anagram'函数。 “是否”字“和”检查“长度相同?”和“是”字和“检查”相同的单词?“在检查字谜时,问的是合乎逻辑的问题。 – Kevin

+0

另外,我认为你有一个bug,因为'anagram(“aab”,“baa”)'返回0. – Kevin

回答

2

我理解你的程序的样子,你想,直到他按Ctrl-d(其导致EOF不断提示的话用户错误并打破循环)?在这种情况下,您应该在循环开始之前只读取一次文件,并在其中构建一个或一组单词。此外,您的try/except语句应该只包含对input的调用,因为这是您的函数中可能发生此异常的唯一地方。

现在到您的主要问题 - 要计算结果数量并相应地打印不同的语句,只需使用列表理解来获取输入的所有字母列表。然后你可以计算字形并将它们结合在一起形成一个输出字符串。

def find_anagrams(): 
    with open("dictionary.txt", "r") as fileInput: 
     words = set(word.strip() for word in fileInput) 

    while True: 
     try: 
      user_input = input("Word? ").strip() 
     except: 
      break #you probably don't care for the type of exception here 

     anagrams = [word for word in words if anagram(word, user_input)] 
     print_results(anagrams) 

def print_results(anagrams): 
    if len(anagrams) == 0: 
     print("there are no anagrams") 
    elif len(anagrams) == 1: 
     print("the only anagram is %s" % anagrams[0]) 
    else: 
     print("there are %s anagrams: %s" % (len(anagrams), ', '.join(anagrams))) 

从这个代码唯一缺少的是cheking,输入字不是结果列表中的一部分,但这是可以移动到anagram功能。该函数也可以使用内置集合模块中的Counter类来简化。这个类是可以从可迭代来构造一个类字典对象,并在迭代的每个对象映射到其出现的次数:

>>> Counter("hello") == {"h":1, "e":1, "l":2, "o": 1} 
True 

因此,我们可以重写字谜函数是这样的:

from collections import Counter 

def anagram(word, check): 
    return not word == check and Counter(word) == Counter(check) 
+0

感谢您的帮助。 – Ace

1

你可以创建你的结果像这样的列表:

with open("dictionary.txt", "r") as fileInput: 
    user_input = input("Search keyword: ").strip() 

    listAnagrams = [] 
    for line in fileInput.readlines(): 
     for word in line.split(" "): 
      if len(word) == len(user_input): 
       if word == user_input: 
        continue 
       elif anagram(word, user_input): 
        listAnagrams.append(word) 

    if len(listAnagrams) == 1: 
     print ('The only anagram for', user_input, 'is', listAnagrams[0]) 

    elif len(listAnagrams) > 1: 
     print ('The anagrams for', user_input, 'are', ", ".join(listAnagrams)) 

    else: 
     print ('No anagrams found for', user_input) 
+0

如果'user_input'很短,会建议生成所有可能的输入组合,并简单地检查哪一个文件中存在组合 – 2012-12-06 20:54:58

+2

只是一个头 - 在python3中'raw_input'已被重命名为'input',python2'input'函数已被删除。 – l4mpi