0

I am looking to execute all permutations of a list of functions, both partial and full, and add the results and put them in a list. For example, say I have three functions:

foo():
 return 1

bar():
 return 2

bat():
 return 3

I want to execute foo(), bar(), bat(), foo() bar(), foo() bat() bar() bat(), and foo() bar() bat().

Therefore, the resulting list would be: [1, 2, 3, 3, 4, 5, 6].

Any idea how I could call all these functions? In reality I will have around 50 functions and want to record all combinations of all functions.

Thank you for any help you give.

3 Answers 3

1

Did you know that you could store functions in lists in Python?

You can define all your functions first:

def foo():
    return 1

def bar():
    return 2

def bat():
    return 3

Then, create a list to hold all of them:

list_of_functions = [foo, bar, bat]

Now you can use itertools.combinations to do what you want:

from itertools import combinations

res_list = []

for i in range(len(list_of_functions)):
    comb_list = list(combinations(list_of_functions, i+1))
    for combination in comb_list:
        x = sum(e() for e in combination)
        res_list.append(x)

print(res_list)
Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming you'd want the functions to be called again for each computed sum, i.e. not computing the function results only once and then compute the sums of their outcomes.

This seems to me like the cleanest approach:

from itertools import combinations


def foo():
    return 1


def bar():
    return 2


def bat():
    return 3


x = [foo, bar, bat]

# literally the list of sums of function results, 
# for functions from all combinations of any length > 0 from x
result = [sum([f() for f in t]) for n in range(len(x)) for t in combinations(x, n+1)]
print(result)

Output:

[1, 2, 3, 3, 4, 5, 6]

Comments

0

Later edit:

from itertools import chain, combinations

l = [foo, bar, bat]

[sum(j() for j in z) for z in chain.from_iterable(([y for y in x] for x in combinations(l, r)) for r in range(1,len(l)+1))]

4 Comments

Note that the result of foo(), bar() and bat() is only computed once - this may not be the result OP wants if these functions would not always return the same value on each call.
@Grismar Thanks for the observation! Went on the once called funcs way because he said about having 50 funcs .. But got your point and included in the answer.
There's another error in your code, it only works for a list of functions of length 3, since you only ask for combinations with a r of len(l)-1. Just try running your solution with 4 or 2 functions.
Another? Which was the first? Also noticed that your answer is the "overcomplicated" as you mentioned, in a list comprehension :)

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.