简体   繁体   English

Python for循环在第一次迭代后返回零

[英]Python for loop returns zero after first iteration

I'm trying to write a program that compares each item in a list against the text of a document. 我正在尝试编写一个程序,将列表中的每个项目与文档文本进行比较。 The program should then return a new list with a value appended to each item of how many times it matched up against a word in the document. 然后,程序应返回一个新列表,其中每个项目附加一个值,该值与文档中的单词匹配的次数。 I have a function written that actually does the matching and it works fine on its own. 我有一个函数编写,实际上匹配,它自己工作正常。 The loop that does the counting also works for single entries. 执行计数的循环也适用于单个条目。 However, when I try to run it for all the entries of the list, it comes back with the proper number for the first list entry and then just gives zeroes back for the rest. 但是,当我尝试为列表的所有条目运行它时,它会返回第一个列表条目的正确数字,然后只返回其余的零。

Here's an idea of what it looks like: 以下是它的外观:

    doc = open("C:/...")
    list = ['string_1', 'string_2', 'string_3', ...]
    answer = []
    ...
    [some code]
    ...
    for t in list:
        counter = 0
        for word in doc:
            if func(word,t) == True:
                counter += 1
        answer.append([counter,t])
    print answer

The closest thing to answering my question was this article. 回答我的问题最接近的是这篇文章。 However, I do want to reset the counter for each list item and I haven't included the "counter = 0" in the actual "for" statement where the calculation is done. 但是,我确实想重置每个列表项的计数器,并且我没有在计算完成的实际“for”语句中包含“counter = 0”。

I have a feeling that it may have to do with the placement of the "counter = 0" assignment, but if I place it outside the "for t in list:" loop, then it just returns the same value for every list entry. 我有一种感觉,它可能与“counter = 0”赋值的位置有关,但是如果我将它放在“for t in list:”循环之外,那么它只为每个列表条目返回相同的值。

Change your first line to this: 将您的第一行更改为:

doc = open("C:/...").read().split()

This should return you a list of all the words in the file. 这应该返回一个文件中所有单词的列表。

The reason it's failing is because when you do for word in doc: it's iterating through the file. 它失败的原因是因为当你for word in doc:执行for word in doc:它正在遍历文件。 So it can only ever be read once. 所以它只能读一次。 If you save the contents of the file to a variable you can iterate over it as many times as you like. 如果将文件的内容保存到变量,则可以根据需要多次迭代它。

This loop is reading to the end of the file 此循环正在读取文件的末尾

for word in doc:
    ...

You'd need to reopen it or seek back to the beginning. 您需要重新打开它或寻求回到起点。

For a quick hack (i guess your program is a quick hack since you are not bothering to close the file), you could use 对于快速破解(我猜你的程序是一个快速的黑客,因为你不打扰关闭文件),你可以使用

doc = list(open("C:/..."))

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

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