0
我遇到一个内存错误在我的递归函数之一。内存错误(Python的2.7.3)
def allPaths(self, adjMat, start, stop, flag=[0], walks=[]):
walks = walks + [start]
if start == stop:
return [walks]
loc = 0
flag=flag*len(adjMat)
output = []
for value in adjMat[start]:
if value > 0.0:
if flag[loc] < 3:
flag[loc]+=1
paths = self.allPaths(adjMat, loc, stop, flag, walks)
for k in paths:
output.append(k)
loc += 1
return output
一个示例输入很好,但我得到了不同矩阵的内存错误。
>>>print test.allPaths([[0.0, 0.9, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0],
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.8, .15, .05, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]],0,7)
[[0, 1, 2, 3, 3, 3, 4, 7], [0, 1, 2, 3, 3, 3, 5, 7], [0, 1, 2, 3, 3, 4, 7], [0, 1, 2, 3, 3, 5, 7], [0, 1, 2, 3, 4, 7], [0, 1, 2, 3, 5, 7], [0, 6, 7]]
>>>print test.allPaths([[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.8, 0.0, 0.0, 0.0, .05, .15, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.9, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0]],0,7)
该错误似乎发生在“flag = flag * len(adjMat)”行。有什么建议么?
我不知道这是否是问题,但有一点你应该避免的是使用可变默认参数,比如'[0]'和'[]',因为它们只在函数定义计算一次(不每次按照您的预期调用函数)。参见[这里]的解释和讨论(http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument)。 – andersschuller