Python 多次调用和检索numpy数组
我的代码是这样的:Python 多次调用和检索numpy数组,python,arrays,numpy,Python,Arrays,Numpy,我的代码是这样的: def something(): """this function will call the `get_array` multiple times until the end of the array and is fixed (cannot change this function)""" #skip some functions and initialization there #only used to get the result of
def something():
"""this function will call the `get_array` multiple times until the
end of the array and is fixed (cannot change this function)"""
#skip some functions and initialization there
#only used to get the result of `get_array`
a.get_array()
在单独的文件中:
class a:
def __init__(self, count=-1):
self.count = count
def cal_array(self, parameters):
"""calculate array"""
return array
def get_array(self, parameters):
if self.count == -1:
self.cal_array(parameters)
# some methods to store array there
else:
self.count += 1
return array[self.count,:]
我想知道是否有一种方法可以计算numpy数组一次并保存它以备将来使用(从另一个函数调用)
我知道我可以将numpy数组保存到磁盘或使用全局dict保存数组,但我想知道是否有更有效的方法来执行此操作。我认为这取决于您希望如何处理
内部的数组。此外,您的示例不正确,或者至少不完整。您不能调用a.get\u array()
,因为它不是一个静态方法,您需要先初始化一个实例。@iled我需要使用get\u array的结果进行一些计算(这应该是在cal\u array
中计算的一行数组。是的,我知道我没有初始化该类,因为something
是另一个类中的函数,我不想编写整个函数,但我会添加一个注释。感谢您指出。