【问题标题】:Receiving a Memory Error?收到内存错误?
【发布时间】:2013-04-26 17:22:33
【问题描述】:

我在我大学的一位教授的办公室工作,他让我通读整个班级的论文,试图抓住抄袭的人,所以我决定使用 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])

由于某种原因,我无法弄清楚自己做错了什么,在我短暂的一学期编程生涯中,我从未收到过内存错误。感谢您的所有帮助。

【问题讨论】:

    标签: python file memory python-3.x directory


    【解决方案1】:

    您可能希望在 while 中增加 index1 而不是 index2 出现错误。将index2+=1 更改为index1+=1

    目前您处于无限循环中,因为 index1 &lt;= len(Words3) 始终为真,因为您不会更改 index1,并且您会追加到 new_list2 直到内存不足。


    这个错误的寓意应该是使用更好的变量名,而不是仅仅将数字附加到现有变量的末尾。这样会降低您输入错误的可能性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多