0

I have to execute a large number of functions, with a variety of different arguments. How can I map a function over a dictionary of functions w/ lists of arguments

Rather than:

a = np.array([1,2,3,4,5,6])

np.mean(a)
np.quantile(a,q=0.5)
np.quantile(a,q=0.5)

Unpack and execute across all combinations in dictionary:

f_dict = { 'maximum':{} ,
          'quantile': [{'q':"0.5"},{'q':'0.95'}]}
 
2
  • 2
    You have a dictionary of str objects mapped to other dict and list objects.... no functions in your dict... did you mean to use functions in your dict? What is the point of this dict? Commented Jun 26, 2020 at 19:05
  • The answer below should give you the desired result, but unless you want to load the dictionary from like a config file or some external source I would advise you to stick to regular function calls for better readability. Commented Jun 26, 2020 at 19:27

1 Answer 1

1

First of all, I recommend using the actual functions as the keys for the dictionary. Also, I recommend formalizing the dictionary values to be a list of dictionaries.

If you did both of these changes, then you can use something like so:

f_dict = { np.mean:[{}] ,
          np.quantile: [{'q':0.25}, {'q':0.5}]}

print([func(a, **arg) for func, args in f_dict.items() for arg in args])
#[3.5, 2.25, 3.5]
Sign up to request clarification or add additional context in comments.

1 Comment

I believe the OP wanted to create a structure where he/she can apply all the functions with arguments all at once. Like map() but with the additional, changeable arguments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.