Python 调用所有可能的函数组合

Python 调用所有可能的函数组合,python,algorithm,permutation,computer-science,combinatorics,Python,Algorithm,Permutation,Computer Science,Combinatorics,假设我有一组函数,每个函数都标记为doThing1、doThing2等,直到doThing10。我想调用这些函数的所有可能组合。不必调用每个函数,但是每个函数只能调用一次。如何在python中高效地实现这一点?用于获取所有排列并将函数放入列表中 以下是一个例子: import itertools def doThing1(): print "thing 1" def doThing2(): print "thing 2" def doThing3(): print

假设我有一组函数,每个函数都标记为doThing1、doThing2等,直到doThing10。我想调用这些函数的所有可能组合。不必调用每个函数,但是每个函数只能调用一次。如何在python中高效地实现这一点?

用于获取所有排列并将函数放入列表中

以下是一个例子:

import itertools

def doThing1():
    print "thing 1"

def doThing2():
    print "thing 2"

def doThing3():
    print "thing 3"

functions = [doThing1, doThing2, doThing3]

for func_list in itertools.permutations(functions):
    for func in func_list:
        func() 
这是它的输出:

$ python permutations.py 
thing 1
thing 2
thing 3
thing 1
thing 3
thing 2
thing 2
thing 1
thing 3
thing 2
thing 3
thing 1
thing 3
thing 1
thing 2
thing 3
thing 2
thing 1
用于获取所有排列并将函数放入列表中

以下是一个例子:

import itertools

def doThing1():
    print "thing 1"

def doThing2():
    print "thing 2"

def doThing3():
    print "thing 3"

functions = [doThing1, doThing2, doThing3]

for func_list in itertools.permutations(functions):
    for func in func_list:
        func() 
这是它的输出:

$ python permutations.py 
thing 1
thing 2
thing 3
thing 1
thing 3
thing 2
thing 2
thing 1
thing 3
thing 2
thing 3
thing 1
thing 3
thing 1
thing 2
thing 3
thing 2
thing 1

你所说的“所有可能的组合”是什么意思?给我们举个例子,比如说,三个函数。还有,你真的是指组合吗?还是说排列?我是说组合,顺序不重要。一个可能的组合是doThing1,doThing3,doThing2,然而另一个可能的组合是doThing1,doThing2或只是doThing1等等,你说的“每个可能的组合”是什么意思?给我们举一个例子,比如说,三个函数。还有,你真的是指组合吗?还是说排列?我是说组合,顺序不重要。一个可能的组合是doThing1、doThing3、doThing2,然而另一个可能的组合doThing1、doThing2或只是doThing1 etcOP要求组合,而不是排列。他还说,并不是每次都要调用每个函数,所以推测
thing1,thing3
的组合是有效的。您需要进行单函数组合、双函数组合和三函数组合。OP要求的是组合,而不是置换。他还说,并不是每次都要调用每个函数,所以推测
thing1,thing3
的组合是有效的。您需要进行单功能组合、双功能组合和三功能组合。