接收内存错误?

2024-10-01 22:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学院一位教授的办公室工作,他让我通读一整堂课的论文,试图抓住抄袭的人,所以我决定用python编写一个程序,查看所有论文中的所有六个单词短语,并对它们进行比较,看是否有任何一篇论文有超过200个匹配的短语。这六个单词的短语将是例如。。。你知道吗

我吃了一个土豆,很好吃。可能是:

我吃了一个土豆和它

吃了一个土豆

一个土豆,很好吃。你知道吗

我的代码当前是

import re
import glob
import os

def ReadFile(Filename):
    try:
        F = open(Filename)
        F2=F.read()
    except IOError:
        print("Can't open file:",Filename)
        return []
    F3=re.sub("[^a-z ]","",F2.lower())
    return F3
def listEm(BigString):
    list1=[]
    list1.extend(BigString.split(' '))
    return list1

Name = input ('Name of folder? ')
Name2=[]
Name3=os.chdir("Documents")
for file in glob.glob("*txt"):
    Name2.append(file)

for file in Name2:
    index1=0
    index2=6
    new_list=[]
    Words = ReadFile(file)
    Words2= listEm(Words)
    while index2 <= len(Words2):
        new_list.append(Words2[index1:index2])
        index1 += 1
        index2 += 1

    del Name2[0]  ##Deletes first file from list of files so program wont compare the same file to itself.

    for file2 in Name2:
        index=0
        index1=6
        new_list2=[]
        Words1= ReadFile(file2)
        Words3= listEm(Words)
        while index1 <= len(Words3):
            new_list2.append(Words3[index:index1])  ##memory error
            index+=1
            index2+=1
    results=[]
    for element in new_list:
        if element in new_list2:
            results.append(element)
    if len(results) >= 200:
        print("You may want to examine the following files:",file1,"and",file2)

我在上收到一个内存错误

new_list2.append(Words3[index:index1])

由于某种原因,我不知道我做错了什么,在我短短的一学期编程生涯中,我从来没有收到过一个记忆错误。谢谢你的帮助。你知道吗


Tags: inimportnewforindexgloblistfile
1条回答
网友
1楼 · 发布于 2024-10-01 22:31:03

您可能希望在while中增加index1而不是index2,并返回错误。将index2+=1更改为index1+=1。你知道吗

当前您处于一个无限循环中,因为index1 <= len(Words3)始终为真,因为您不更改index1,并且您附加到new_list2,直到内存用完为止。你知道吗


这个错误的寓意应该是使用更好的变量名,而不仅仅是在现有变量的末尾加上数字。这种方法可以降低像你这样输入错误的可能性。你知道吗

相关问题 更多 >

    热门问题