简体   繁体   English

python for循环未执行

[英]python for-loop not executed

I try to write a python script that searches a txt file (english dictionary) for anagrams. 我尝试编写一个在txt文件(英语词典)中搜索字谜的python脚本。 I have those three functions: 我有这三个功能:

def is_anagram(a,b):
    a_ = list(a)
    a_.sort()
    b_ = list(b)
    b_.sort()
    if a_ == b_ and a != b:
        return True
    else:
        return False

def find_anagrams(word,t):
    _res=[word]
    for line in t:
        check = line.strip()
        if is_anagram(check,word):
            _res += [check]
    return _res

def find_all_anagrams(f):
    res = {}
    void = []
    for line in f:
        word = line.strip()
        _list = list(word)
        _list.sort()
        key = tuple(''.join(_list))
        if key not in res and key not in void:
            if find_anagrams(word,f) == []:
                void += [key]
            res[key] = find_anagrams(word,f)
    return res

If i call the find_all_anagrams function with: 如果我使用以下命令调用find_all_anagrams函数:

fin = open ('words.txt')
print find_all_anagrams(fin)

The program stops after the first loop and just gives me 该程序在第一个循环后停止,只是给了我

{('a', 'a'): ['aa']}

Why does it not continue and process the second line of words.txt? 为什么不继续并处理word.txt的第二行? Btw the words.txt file is the one from Moby Project that can be downloaded here( http://thinkpython.com/code/words.txt ) 顺便说一句,word.txt文件是来自Moby Project的文件,可以在此处下载( http://thinkpython.com/code/words.txt

When you call find_all_anagrams it will read the first line from file. 当您调用find_all_anagrams ,它将从文件中读取第一行。 Then it will call find_anagrams which will read the rest of the file. 然后它将调用find_anagrams ,它将读取文件的其余部分。 When the for loop in find_all_anagrams tries to pull next line from the file there's nothing more to read so it returns with the result generated so far. find_all_anagramsfor循环尝试从文件中提取下一行时,没有更多要读取的内容,因此它将返回生成的结果。

Even if you'd change your program so that find_all_anagrams would continue from the following line it would be horribly slow because the time complexity is O(n^2) . 即使您要更改程序,以便find_all_anagrams从下一行继续执行,这也非常慢,因为时间复杂度为O(n ^ 2) Instead you could read the file once and store the words to dictionary where key is the sorted word and value is a list of words: 相反,您可以一次读取文件,并将单词存储到字典中,其中key是排序的单词,value是单词列表:

from collections import defaultdict

def key(word):
    return ''.join(sorted(word))

d = defaultdict(list)
with open('words.txt') as f:
    for line in f:
        line = line.strip()
        d[key(line)].append(line)

print d[key('dog')]

Output: 输出:

['dog', 'god']

From within find_all_anagrams(f) you then pass f to find_anagrams(word,f) . 从内find_all_anagrams(f)时,传递ffind_anagrams(word,f) In find_anagrams it then iterates over all the lines of the file on the line for line in t: 然后在find_anagramsfor line in t:的行上遍历文件的所有行for line in t:

By the time it returns to find_all_anagrams, it's already read the entire file, and there is nothing left to read. 当它返回到find_all_anagrams时,它已经读取了整个文件,没有什么可读取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM