Anagram函数返回无

时间:2015-11-13 01:25:56

标签: python function file if-statement for-loop

我必须创建一个调用函数的程序,并从文件中搜索字符串的所有字符,并返回单词列表。

我做了一切,它应该有效,但是当我开始时,它给了我None。我甚至尝试过文件中的单词并且它是一样的。

def find_anagrams_in_wordlist(str, str_list):
    str_list = get_dictionary_wordlist()    

    for int in range (0, len(str)):
        anagram(str, str_list[int])
        if anagram(str, str_list[int]):
            return(str_list[int])

def find_anagrams(str):
    str_list = get_dictionary_wordlist()

    return find_anagrams_in_wordlist(str, str_list)

def test_find_anagrams():
    print(find_anagrams("tenato"))

这是我的anagram()功能:

def anagram(str1, str2):
    str1_list = list(str1)
    str1_list.sort()
    str2_list = list(str2)
    str2_list.sort()

    return (str1_list == str2_list)

这是我的get_dictionary_wordlist()功能:

def get_dictionary_wordlist(): 
    text_file = open("dictionary.txt", "r") 
    return text_file.read().splitlines() 

我应该改变什么来使其有效?

2 个答案:

答案 0 :(得分:1)

好的,先猜一下;这段代码:

def find_anagrams_in_wordlist(str, str_list):
    str_list = get_dictionary_wordlist()    

    for int in range (0, len(str)):
        anagram(str, str_list[int])
        if anagram(str, str_list[int]):
            return(str_list[int])

正在使用range(0, len(str)) - 'tenato'中的字符数 - 而不是range(0, len(str_list)) - 字典中的字数。

这意味着您只测试前几个字典单词,忽略其余单词。试一试:

def find_anagrams_in_wordlist(str, str_list):
    str_list = get_dictionary_wordlist()    

    for word in str_list:
        if anagram(str, word):
            return word

(不需要使用range()来计算Python中的列表,你可以直接for item in mylist:

NB。如果这样做,你的设计仍将只返回匹配的第一个单词,而不是匹配的单词列表。您需要建立匹配列表,然后在循环完成后返回列表。

答案 1 :(得分:0)

让我们检查一下你有什么:

def find_anagrams_in_wordlist(str, str_list):  # You shouldn't name something 'str' because it's a type.
    str_list = get_dictionary_wordlist()  # You are overwriting your incoming list with the same function call.

    for int in range (0, len(str)):  # You're executing your for loop once per letter in the candidate word.
        anagram(str, str_list[int])  # This function executes but isn't doing anything
        if anagram(str, str_list[int]):
            return(str_list[int])  # If you find something you are exiting your for loop AND your function

def anagram(str1, str2):
    str1_list = list(str1)
    str1_list.sort()  # The result of this is the same every time... and it's None
    str2_list = list(str2)
    str2_list.sort()  # Always returns None

    return (str1_list == str2_list)  # This is the only real work being done here.

最后,您实际上并未累积结果:

def find_anagrams_in_wordlist(candidate, word_list):  # Name your variables clearly
    results = list()  # For accumulating your results
    sorted_candidate = sorted(candidate)  # You only need to sort your candidate once
    for word in word_list:  # This is the cleanest way to loop through an iterable
        sorted_word = sorted(word)  # Sort the word once
        if sorted_candidate == sorted_word:  # Now check for equality: an additional function can obfuscate things
            results.append(word)  # If it's equal, add it to your results
    return results  # Send back your results

我们可以在REPL中测试它:

>>> lst = {"cat", "dog", "fish", "god"}
>>> find_anagrams_in_wordlist("odg", lst)
['god', 'dog']
>>> find_anagrams_in_wordlist("cat", lst)
['cat']
>>> find_anagrams_in_wordlist("shif", lst)
['fish']
>>> find_anagrams_in_wordlist("birds", lst)
[]

请注意,在我的解决方案中,我使用sorted而不是sort()sort()无法完成正确的事情:

>>> x = "moonshot"
>>> y = list(x)
>>> y
['m', 'o', 'o', 'n', 's', 'h', 'o', 't']
>>> z = y.sort()
>>> z
>>> type(z)
<type 'NoneType'>
>>> sorted(x)
['h', 'm', 'n', 'o', 'o', 'o', 's', 't']