向数组列动态添加函数



我试图动态添加函数调用来填充数组列。我将访问数组数百万次,所以它需要快速。

我正在考虑通过使用字符串变量

将函数调用添加到字典中
numpy_array[row,column] = dict[key[index containing function call]]

我正在使用的代码的全部范围太大,不能在这里发布一个我尝试过的同等简单的示例。

def hello(input):
return input
dict1 = {}
#another function returns the name and ID values
name = 'hello'
ID = 0
dict1["hi"] = globals()[name](ID)
print (dict1)

但是当使用

时它实际上激活了函数
globals()[name](ID) 

而不是将hello(0)作为变量复制粘贴到字典中。

我有点力不从心。实现这一点的正确方法是什么?

是否有比每次调用

时读入字典更有效的方法?
numpy_array[row,column] = dict[key[index containing function call]]

因为我将访问和更新它数百万次

我不知道是否每次写入数组时都调用字典,或者列的位置是否已经保存到缓存中。

感谢你的帮助。

编辑

最终我要做的是用函数

初始化一些数组,字典和值
def initialize(*args):
create arrays and dictionaries
assign values to global and local variables, arrays, dictionaries

每次使用initialize()函数时,它都会创建一组新的变量(名称,值等),这些变量指向具有不同变量集的不同函数。

我有一个numpy数组,我想存储函数的信息和initialize()函数创建的相关值。

换句话说,在上面的例子中,hello(0),函数名,它的值,以及initialize()

中设置的其他一些东西我要做的是在运行主程序之前,将具有这些设置的函数添加到numpy数组中作为新列。

作为另一个例子。如果我正在设置hello()(而hello()是一个复杂的函数),当我使用initialize()时,它可能会给我hello(1)的值为1。然后如果我再次使用initialize它可能会给我hello(2)的值2。如果我再次使用它,它可能会为函数goodbye(0)提供值0。

在这种情况下假设我有一个数组

array[row,0] = stuff()
array[row,1] = things()
array[row,2] = more_stuff()
array[row,3] = more_things()

现在我想让它看起来像

array[row,0] = stuff()
array[row,1] = things()
array[row,2] = more_stuff()
array[row,3] = more_things()
array[row,4] = hello(1)
array[row,5] = hello(2)
array[row,6] = goodbye(0)

第三个例子。

def function1():
do something

def function2():
do something

def function3():
do something

numpy_array(size)
initialize():
do some stuff

then add function1(23) to the next column in numpy_array

initialize():
do some stuff

then add function2(5) to the next column in numpy_array


initialize():
do some stuff

then add function3(50) to the next column in numpy_array

如你所见。我需要永久地将新列附加到数组中,并按照initialize()函数的指示为新列提供函数/值,而无需手动干预。

所以从根本上说,我需要弄清楚如何根据字符串值为数组列分配语法,而不激活分配时的语法。

编辑# 2

我想我的解释不够清楚。这是另一种看待它的方式。

我正在尝试根据函数的输出动态地将函数分配给numpy数组中的另一列。

添加到数组列中的函数将用于向数组中填充数百万次数据。

添加到数组中的函数可以是各种不同的函数,具有各种不同的输入值,添加的函数的数量可以变化。

我已经尝试使用exec(), eval()和globals()将函数分配给字典,但是当在分配期间使用这些函数时,它只是立即激活函数而不是分配它们。

numpy_array = np.array((1,5))
def some_function():
do some stuff
return ('other_function(15)')
#somehow add 'other_function(15)' to the array column.
numpy_array([1,6] = other_function(15)

每次程序运行时,some_function()返回的函数可能存在,也可能不存在,因此添加到数组中的函数也是动态的。

我不确定这是OP之后的内容,但这里有一种方法可以通过名称间接表示函数:

def make_fun_dict():
magic = 17

def foo(x):
return x + magic

def bar(x):
return 2 * x + 1

def hello(x):
return x**2

return {k: f for k, f in locals().items() if hasattr(f, '__name__')}
mydict = make_fun_dict()
>>> mydict
{'foo': <function __main__.make_fun_dict.<locals>.foo(x)>,
'bar': <function __main__.make_fun_dict.<locals>.bar(x)>,
'hello': <function __main__.make_fun_dict.<locals>.hello(x)>}
>>> mydict['foo'](0)
17

使用例子:

x = np.arange(5, dtype=int)
names = ['foo', 'bar', 'hello', 'foo', 'hello']
>>> np.array([mydict[name](v) for name, v in zip(names, x)])
array([17,  3,  4, 20, 16])

最新更新