我有一组要作为参数应用于函数的值:
params = {
'a': [1, 2, 3],
'b': [5, 6, 7],
'x': [None, 'eleven', 'f'],
# et cetera
}
我想用所有可能的组合运行myfunc()
,所以myfunc(a=1, b=5, x=None ...)
,myfunc(a=2, b=5, x=None ...)
。。。myfunc(a=3, b=7, x='f' ...)
。有什么东西(例如itertools
)可以帮助你吗?我考虑过使用itertools.product()
,但它没有保留参数的名称,只给了我组合的元组。在
可以使用^{} 获取参数的所有组合:
使用Argument list unpacking,可以使用关键字参数的所有组合调用
^{pr2}$myfunc
:输出:
.keys
和.values
的排序在所有Python版本中都得到了保证(除非dict被更改,而这在这里不会发生),因此这可能有点微不足道:您可以在docs中找到担保:
演示:
^{pr2}$相关问题 更多 >
编程相关推荐