我曾经在32位操作系统上运行32位python,每当我意外地将值附加到无限列表中的数组或者尝试加载太大的文件时,python就会因内存不足而停止运行.但是,我现在在64位操作系统上使用64位python,而不是给出异常,python使用了最后一点内存并导致我的计算机冻结,所以我不得不重新启动它.
我查看了堆栈溢出,似乎没有一种好方法来控制内存使用或限制内存使用.例如,这个解决方案:如何在python中设置线程或进程的内存限制?限制python可以使用的资源,但是粘贴到我想写的每段代码中都是不切实际的.
我怎样才能防止这种情况发生?
我想通过元素,而无需使用numpy的列表元素上进行操作,例如,我想add([1,2,3], [2,3,4]) = [3,5,7]和mult([1,1,1],[9,9,9]) = [9,9,9],但我不知道它在做的方式是,它认为"正确"的风格.
我提出的两个解决方案是
def add(list1,list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]+list2[x])
return list3
def mult(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]*list2[x])
return list3
def div(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]/list2[x])
return list3
def sub(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]-list2[x])
return list3
Run Code Online (Sandbox Code Playgroud)
每个操作员都有一个单独的功能
和
def add(a,b)
return a+b
def mult(a,b)
return a*b
def div(a,b)
return a/b
def sub(a,b)
return a-b …Run Code Online (Sandbox Code Playgroud) python functional-programming coding-style list higher-order-functions