简体   繁体   English

Python 内存 - Python 代码超出了内存限制

[英]Python memory - The memory limit exceeded for Python code

I got this problem while I was solving a problem of self crossing.我在解决自穿越问题时遇到了这个问题。 It's like the snake game.这就像蛇游戏。

Given x = [2, 1, 1, 2], we start from 0, and then we go up for 2, go left for 1, go right for 1 and go down for 2. The goal is to calculate if it will self cross or not.给定 x = [2, 1, 1, 2],我们从 0 开始,然后向上走 2 次,向左走 1 次,向右走 1 次,向下走 2 次。目标是计算它是否会自交叉与否。 As the given steps are always integers, I thought about using a matrix and every time when it get through a point, I set the point to 1. Like this I could check if the point has been visited or not.由于给定的步骤总是整数,我考虑使用矩阵,每次当它通过一个点时,我将点设置为 1。这样我可以检查该点是否已被访问。

I think in this way, all the memory that Python will make is just the assignment of the matrix.我认为这样一来,Python 会做的所有内存都只是矩阵的赋值。 That's why I don't understand when the input x is huge, and why it says "memory out of limit" .这就是为什么我不明白什么时候输入 x 很大,以及为什么它说"memory out of limit" For me, I think the memory is the same as the matrix has been initiated before.对我来说,我认为记忆与之前启动的矩阵相同。

How can I fix this?我怎样才能解决这个问题?

class Solution(object):

    def isSelfCrossing(self, x):
        """
        :type x: List[int]
        :rtype: bool
        """
        m = [[0 for j in xrange(1000)] for j in xrange(1000)]
        m[0][0] = 1
        a, b = 0, 0
        flag = 0
        for i in x:
            flag = (flag + 1) % 4
            if flag == 1:
                for temp in xrange(1, i+1):
                    b += 1
                    if m[a][b] != 0:
                        return True
                    else: m[a][b] = 1
            elif flag == 2:
                for temp in xrange(1, i+1):
                    a -= 1
                    if m[a][b] != 0:
                        return True
                    else: m[a][b] = 1
            elif flag == 3:
                for temp in xrange(1, i+1):
                    b -= 1
                    if m[a][b] != 0:
                        return True
                    else: m[a][b] = 1
            else:
                for temp in xrange(1, i+1):
                    a += 1
                    if m[a][b] != 0:
                        return True
                    else: m[a][b] = 1
        return False

How huge could "x" be? “x”可以有多大? If "x" is not so big, does your code run successfully?如果“x”不是那么大,你的代码运行成功了吗?

Assuming "x" is really really huge, it might be related to your "m" initialization which is the straw that broke the camel's back .假设“x”真的很大,它可能与你的“m”初始化有关,这是压倒骆驼的稻草 In other words, your "x" (and some other memory allocations) might consume almost all of the allowed memory allocation of the process, and "m" initialization passes over the limit.换句话说,您的“x”(以及其他一些内存分配)可能消耗了进程允许的几乎所有内存分配,并且“m”初始化超过了限制。

Using lists for a boolean matrix in Python rarely makes sense.在 Python 中为布尔矩阵使用列表很少有意义。 Try storing pairs of integers in a set and replace尝试将整数对存储在一个set并替换

m = ...       # with m = set()
m[a][b] != 0  # with (a, b) in m
m[a][b] = 1   # with m.add((a, b))

Alternatively, you can create a flat bytearray with the same number of elements as m and index it as a * 1000 + b .或者,您可以创建一个元素数与m相同的平面bytearray ,并将其索引a * 1000 + b This, however, might lead to subtle indexing bugs, so I strongly suggest to give set a try first.然而,这可能会导致微妙的索引错误,所以我强烈建议先尝试set

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

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