如何在python中提高内存限制?

时间:2015-11-11 22:43:21

标签: python python-2.7 out-of-memory

我编写了一个python(2.7)脚本,但它使用了大量内存,因此出现内存不足错误。是否可以使用内存?

我的代码(or the github):

from itertools import combinations
import numpy    
# Find the unused members and put this in a other group
def findMembers(listIn,listMembers):
    lengthlist2 = (len(listMembers)-len(listIn[0]))
    group2 = [0] * lengthlist2 #making the other groups based on the length of the first group
    for i in listIn:
        wichRow = 0
        for x in listMembers:
            if not (x in i) :
                group2[wichRow] = x
                wichRow += 1
    listIn.append(group2)
    return listIn

#you give a list of members and the numbers of groups
#you get back all the possibilities of combinations
def findCombinations(listMembers,numbersOfGroups):
    groupTemp = [] #list needed to save correctly all the combinations
    group = [] #list needed for keep it simple
    newGroup = [] #list that will be returned
    for listPossibilities in combinations(listMembers,(len(listMembers)/numbersOfGroups)):
        groupTemp.append(list(listPossibilities))
        group.append(groupTemp) #saving all the possibilities 
        groupTemp = []
    for k in group:
        # place the unused members in group2
        k = (findMembers(k,listMembers))
        if numbersOfGroups > 2:
            groupTemp = []
            groupTemp = findCombinations(k[1],numbersOfGroups-1)
            for i in groupTemp:
                listTemp = []
                listTemp.append(k[0])
                listTemp.extend(i)
                newGroup.append(listTemp)
        else:
            newGroup = group
    return newGroup

    # Calculate the happiness of the group
def findHappiness(tabel,listIn):
    happiness = 0
    for i in listIn:
        for j in i:
            for k in i:
                happiness += tabel[j][k]    
    return happiness
def buildTabel(members): #build a random survey
    tabel = numpy.random.random((members,members)) 
    return tabel
def calculateHappiness(group):
    print "Finding all the happiness: "
    maxhappiness = 0
    i = 0
    for x in group:
        happiness = findHappiness(tabel,x)
        if happiness > maxhappiness:
            maxhappiness = happiness
            y = x
        progress = int(round((((i)*1.0/(len(group)))*100.0)))
        update_progress(progress)
        i += 1  
    print "\n Best solution: ", y, " with: ", maxhappiness, " happiness"
def update_progress(progress):
    print '\r[{0}] {1}%'.format('#'*(progress/5), progress),
if __name__ == "__main__":
    members = 24    # members of the group
    numbersOfGroups = 3
    tabel = buildTabel(members) #preferences will be stored here  
    listMembers = (range(members)) #members of the group that need to be divided
    print "Searching all the combinations..."
    group = findCombinations(listMembers,numbersOfGroups) #find all the combinations (recursive)
    print len(group)," combinations"
    calculateHappiness(group) #calculate the most happiest group and print

错误:

Searching all the combinations...
Traceback (most recent call last):
  File "main.py", line 75, in <module>
    calculateHappiness(group) #calculate the most happiest group and print
  File "main.py", line 38, in findCombinations
    newGroup = group
MemoryError

我使用Windows 10 64bit和6gb ram。是否可以使用我的硬盘驱动器磁盘的虚拟RAM或磁盘空间?

0 个答案:

没有答案