简体   繁体   English

将值的组合作为函数的参数传递

[英]Pass a combination of values as function's parameters

i want to pass the parameters as the combination of a,b,c,d values我想将参数作为 a,b,c,d 值的组合传递

a = [1,2,3,4,5,6,7,8,9]
b = [5,6,7,8,9,10,11,12,13,14,15]
c = [1,2,3,4,6,12,18]
d = [1,2,3,4,6,12,18,24]
results = []

this is my funcion:这是我的功能:

def calc(a,b,c,d):
 ....

i want it to run calc(1,5,1,1) and then calc(1,5,1,2) ... calc(1,5,1,24) , calc(1,5,2,1) ... calc(1,5,2,24) ... calc(1,6,1,1) ... calc(1,6,18,24) ... calc(2,5,1,1) ... calc(2,5,1,24) ... until calc(9,15,18,24)我希望它运行calc(1,5,1,1)然后calc(1,5,1,2) ... calc(1,5,1,24) , calc(1,5,2,1) ... calc(1,5,2,24) ... calc(1,6,1,1) ... calc(1,6,18,24) ... calc(2,5,1,1) ... calc(2,5,1,24) ... 直到calc(9,15,18,24)

i want to pass all possible combinations of a,b,c and d as parameters to the function我想将 a、b、c 和 d 的所有可能组合作为参数传递给 function

i have made it by using nested loops我通过使用嵌套循环做到了

for i in a:
    for j in b:
        for k in c:
             for l in d:
                 results.append(calc(i,j,k,l))

but i think this is not the best solution但我认为这不是最好的解决方案

it takes 15 min running because the dataset i'm using is too big运行需要 15 分钟,因为我使用的数据集太大

To tame the complexity a little bit, you can use itertools.product :为了稍微降低复杂性,您可以使用itertools.product

itertools.product(*iterables, repeat=1)

Cartesian product of input iterables.输入迭代的笛卡尔积。

Roughly equivalent to nested for-loops in a generator expression.大致相当于生成器表达式中的嵌套 for 循环。 For example, product(A, B) returns the same as (x,y) for x in A for y in B) .例如, product(A, B)返回与(x,y) for x in A for y in B)相同的结果。

import itertools

p = itertools.product(a, b, c, d)

for value_a, value_b, value_c, value_d in p:
    result = calc(value_a, value_b, value_c, value_d)

    print(result)

The runtime can't really be changed if you need to evaluate everything - but that would depend on your calc function and whether you have any repeats in your inputs (which would allow you to use dynamic programming and cache lookups to your function instead).如果您需要评估所有内容,则无法真正更改运行时 - 但这取决于您的calc function 以及您的输入中是否有任何重复(这将允许您使用动态编程和缓存查找来代替您的 function)。

For simplifying iteration, you can use itertools.product :为了简化迭代, 您可以使用itertools.product

Cartesian product of input iterables.输入迭代的笛卡尔积。

Roughly equivalent to nested for-loops in a generator expression.大致相当于生成器表达式中的嵌套 for 循环。 For example, product(A, B) returns the same as ((x,y) for x in A for y in B).例如,product(A, B) 返回与 ((x,y) for x in A for y in B) 相同的结果。

So to iterate over the product of all these lists:所以要遍历所有这些列表的产品:

import itertools

for a_i, b_i, c_i, d_i in itertools.product(a, b, c, d):
    calc(a_i, b_i, c_i, d_i)

However be aware that this might not be the exact same iteration pattern as you mentioned in your example, but it will still iterate over each possible combination from your lists.但是请注意,这可能与您在示例中提到的迭代模式不完全相同,但它仍会遍历列表中的每个可能组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM