2021-08-30

Dynamically adding functions to array columns

I'm trying to dynamically add function calls to fill in array columns. I will be accessing the array millions of times so it needs to be quick.

I'm thinking to add the call of a function into a dictionary by using a string variable

numpy_array[row,column] = dict[key[index containing function call]]

The full scope of the code I'm working with is too large to post here is an equivalent simplistic example I've tried.

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)

but it literally activates the function when using

globals()[name](ID) 

instead of copy pasting hello(0) as a variable into the dictionary.

I'm a bit out of my depth here. What is the proper way to implement this?

Is there a more efficient way to do this than reading into a dictionary on every call of

 numpy_array[row,column] = dict[key[index containing function call]]

as I will be accessing and updating it millions of times.

I don't know if the dictionary is called every time the array is written to or if the location of the column is already saved into cache.

Would appreciate the help.

edit

Ultimately what I'm trying to do is initialize some arrays, dictionaries, and values with a function

def initialize(*args):
  create arrays and dictionaries
  assign values to global and local variables, arrays, dictionaries

Each time the initialize() function is used it creates a new set of variables (names, values, ect) that direct to a different function with a different set of variables.

I have an numpy array which I want to store information from the function and associated values created from the initialize() function.

So in other words, in the above example hello(0), the name of the function, it's value, and some other things as set up within initialize()

What I'm trying to do is add the function with these settings to the numpy array as a new column before I run the main program.

So as another example. If I was setting up hello() (and hello() was a complex function) and when I used initialize() it might give me a value of 1 for hello(1). Then if I use initialize again it might give me a value of 2 for hello(2). If I used it one more time it might give the value 0 for the function goodbye(0).

So in this scenaro let's say I have an array

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

Now I want it to look like

    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)

As a third, example.

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

So as you can see. I need to permanently append new columns to the array and feed the new columns with the function/value as directed by the initialize() function without manual intervention.



from Recent Questions - Stack Overflow https://ift.tt/3mJj8nO
https://ift.tt/eA8V8J

No comments:

Post a Comment