Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Is there a Python function for mixed additive and multiplicative combination of parameters? [closed]

$
0
0

I'm looking for a Python function or method that can handle both additive and multiplicative combinations of parameters.

Specifically, I want to first apply zip to certain parameters, then apply product to the results of those additions with other parameters and keep the order.

Example:

>> f = [["a1", "a2"], ["b1", "b2"], ["c1", "c2"]]>> comb = "+x+">> len(comb) == len(f)   True>> for i in zip(*f):>>     print(i)   (a1, b1, c1)   (a2, b2, c2)>> for i in product(*f):>>     print(i)   (a1, b1, c1)   (a1, b1, c2)   (a1, b2, c1)   (a1, b2, c2)   (a2, b1, c1)   (a2, b1, c2)   (a2, b2, c1)   (a2, b2, c2)>> iter = func(f, comb)>> for i in iter:>>     print(i)   (a1, b1, c1)   (a1, b2, c1)   (a2, b1, c2)   (a2, b2, c2)

+ means "add". x means "prod". If only "add", zip(*) could solve this and generate 2 items. If only "prod", product(*) handles this situation well and generate 8 items.

Here, waht I need is 2×2=4 ([(a1, c1), (a2, c2)]×[b1, b2]), first zip then product and keep the order, (a, b, c).

for-loops are pythonic, but to solve this problem, only for-loops seem not pythonic enough.

Does Python have a built-in function for this, or is there a recommended approach to achieve this?Any example code would be highly appreciated.;)


Updated:

zip the indices where comb is '+' and product with the rest could solve this problem partially.

zip returns an iterator of tuples. To keep the order, you have to remember the order first, then unpack and reorder the zip&product outputs (e.g., (("a1", "c1"), "b1")).


Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>