python 3:如何在可能参数列表上运行函数并返回所有结果的字典

python 3:如何在可能参数列表上运行函数并返回所有结果的字典,python,python-3.x,lambda,functional-programming,decorator,Python,Python 3.x,Lambda,Functional Programming,Decorator,我正在测试一个函数。我想知道关于各种参数的结果是什么 如果函数只有一个参数,则很容易: def func_a(a): return a+1 def test_func(func,parameter_list): return {t:func(t) for t in parameter_list} print (test_func(func_a,[2,4,5])) 印刷品 {2:3,4:5,5:6} 在这里,我使用两个参数: def strategy_2(a,b):

我正在测试一个函数。我想知道关于各种参数的结果是什么

如果函数只有一个参数,则很容易:

def func_a(a):
    return a+1

def test_func(func,parameter_list):    
    return {t:func(t) for t in parameter_list}

print (test_func(func_a,[2,4,5]))
印刷品

{2:3,4:5,5:6}

在这里,我使用两个参数:

def strategy_2(a,b):
     return a+b

def test_2_strategies(strategy,list_of_lists):
    result={}
    for l1 in list_of_lists[0]:
        for l2 in list_of_lists[1]:
            result[(l1,l2)]=strategy_2(l1,l2)
    return result

print (test_2_strategies(strategy_2,[[1,2,3],[0.3,0.6]]))
结果是:

{(1,0.3):1.3,(1,0.6):1.6,(2,0.3):2.3,(2,0.6):2.6,(3,0.3): 3.3,(3,0.6):3.6}

太好了

但是,如果我想做一个类似的函数,其中列表列表可以有n个列表。并测试所有的组合

我查看了decorators,lambda,functools.partial,但我似乎无法做到这一点

例如,我试过:

def func_b(a,b):
    return a+b

def test_func(func,parameter_list):    
    return {t:func(t) for t in parameter_list}

def test_strategy_many_parameters(func,parameter_lists):
    if len (parameter_lists)>1:        
        test_strategy_many_parameters(test_func(lambda x: func(x,y),parameter_lists[0]), parameter_lists[1:])
    else:                              
        return test_func(func,parameter_lists[0])
但它不起作用

我现在看看这个问题:

但我不知道如何在这种情况下应用它

我想要这样的东西:

def func(x1,x2,...,xn):
    result=...
    return result

def test_func(func,list_of_lists):
    ...

print(test_func(func,[[x1min,...,x1max],[x2min,...,x2max],...,[xnmin,...,xnmax]])
{arg: func(*arg) for arg in itertools.product(*list_of_lists)}
结果是:

{(x1min,x2min,…,xnmin):func(x1min,x2min,…,xnmin),,(x1max, x2max,…,xnmax):func(x1max,x2max,…,xnmax)}

什么是优雅的方式(或者仅仅是一种方式)


谢谢。

不需要列表,只需使用元组列表(元组作为函数参数),就可以像基本情况一样简单

def func(a,b,c):
    result=...
    return result
my_tuple = (1,2,3)


func(*my_tuple)

查看itertools.product以生成参数,然后只需调用函数:

arguments = itertools.product(list_of_values_for_arg_1, list_of_values_for_arg_2, ..., list_of_values_for_arg_n)
# or
arguments = itertools.product(*list_of_lists)
for arg in arguments:
    result=func(*arg)
因此,要生成dict,您需要执行以下操作:

def func(x1,x2,...,xn):
    result=...
    return result

def test_func(func,list_of_lists):
    ...

print(test_func(func,[[x1min,...,x1max],[x2min,...,x2max],...,[xnmin,...,xnmax]])
{arg: func(*arg) for arg in itertools.product(*list_of_lists)}

如果
l
是嵌套列表,则
map(func,*l)
将包含函数
func
中所有o/p的列表,其中包含必需的参数,
{e:func(*e)for e in zip(*l)}
将以dict格式包含结果

>>> def func(*args):
>>>    return f'O/P of func: {args}'
>>>
>>> l = [['x1min','x1max'],['x2min','x2max'],['xnmin','xnmax']]
>>> [*map(func, *l)]
["O/P of func: ('x1min', 'x2min', 'xnmin')", "O/P of func: ('x1max', 'x2max', 'xnmax')"]
>>> 
>>> {e:func(*e) for e in zip(*l)}
{('x1min', 'x2min', 'xnmin'): "O/P of func: ('x1min', 'x2min', 'xnmin')", ('x1max', 'x2max', 'xnmax'): "O/P of func: ('x1max', 'x2max', 'xnmax')"}
>>> 

对不起,我不明白。我尝试了v=(1,2)def func(a,b):返回a+b func(v)给出了一个错误我编辑了我的答案。如上所述调用,元组将充当参数。元组可以是0..n长(长度应与函数参数编号匹配),谢谢。当我意识到我需要集合的笛卡尔积时,我正在看它。唯一的问题是,我并不特别喜欢将所有函数都更改为(*arg)格式,因为这会降低它们的可读性。您不需要这样做。如果列表列表正好包含函数的参数数量,那么*arg将很好地解包。例如:
def(a,b)
可以这样调用:
f(*[1,2])
;a设置为1,b设置为2。(与元组相同)如果您需要使用不同数量的参数测试多个函数,我可以在12小时内向您发送代码,当我到达我的pc时。基本思想是创建参数的dict name_:列出_If_值,然后迭代需要测试的每个函数,通过inspect模块获取函数参数列表,选择必要的值列表,将乘积应用于它们,并通过**kwargs展开将结果参数传递给函数。inspect模块的想法很有趣。我甚至不知道它的存在。谢谢现在不需要了。但是你让我在我的学习清单上加了点别的谢谢。有些东西不能说服我。zip通常适用于长度相同的列表,但这里没有说明不同的子集具有相同的长度。@PietroSperoni。如果子集的长度不同,zip可以替换为
itertools.zip\u longest
。我假设它们都是相同的长度,根据OP,
func
接受固定数量的n参数,但我不相信。使用zip,您可以从每个列表中选择一个列表。但你要遵守秩序。而这里你需要笛卡尔积。这就是为什么另一个使用itertool.product的解决方案可以工作的原因。