Let's say that I have this list :
[2,3,5,7]
I want to output all combinations of multiplication :
[6,10,14,15,21,35,30,42,105,210]
Is there a one liner in python for that?
Assuming that you forgot the 70 in your output...
With numpy.prod
and itertools.combinations
:
>>> from numpy import prod
>>> from itertools import combinations
>>> lst = [2,3,5,7]
>>> [prod(x) for i in range(2, len(lst)+1) for x in combinations(lst, i)]
[6, 10, 14, 15, 21, 35, 30, 42, 70, 105, 210]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With