Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

methods - How to find sum of different combinations of all products in list (python)

I have the following two python lists of numbers:

list1 = [0, 2, 5, 10, 20, 50, 100]
list2 = [2, 4, 6, 8, 10, 20]

I can get the products of each like below:

combo = list(product(list1, list2))

I then have a class where I have created a method which is just simply the plus or minus of the product (dependent on other class variables) of a given element from the two lists, e.g.

class1 = class.method(x, y)
class1 = class.method(list1[1], list2[5])
class1 = class.method(2, 20)
class1 = 40

I want to get the sum of all instances I have of the class for all possible combinations of the products. These can be repeated (i.e. (2, 20) can be used more than once, and so on) so the amount of combinations will be very large.

My issue is that I am not sure how to loop through the whole combined list when my number of instances grows to a very large amount. The only thing I can think of so far is as follows which just results in blanks.

pos_combos = []
for i in combos:
   x, y = i
   if class1.method(x, y)
      +class2.method(x, y)
      +class3.method(x, y)
   …
      +class98.method(x, y)
      +class99.method(x, y)
      +class100.method(x, y) > 0:
         pos_combos.append(i)
print(pos_cases)

I can get the different combinations I want to use doing the below.

combo2 = list(product(combo, repeat=100))

But this is too long to even print, not to mention the issue of passing each element of this through the equation.

Does anyone have any ideas on how to do this? Am thinking that its maybe too big a task for simple for loop/function and some more sophisticated method may be used (i.e. some form of ML). Maybe I could keep the two lists separate and loop through each somehow?

If someone could even point me in right direction it would be greatly appreciated

Thanks.


EDIT: a minimum reproducible example would be as follows: if every second instance was a minus product, given that tuples can be repeated, one example would be if bet1 used (2, 20) and bet2 - bet100 used (2, 5) (so 99 times) Then one entry into pos_combos list would be the tuple of tuples

[((2, 20), ... (2, 5))]

As the sum of these is > 0 as it equals 40. I want to get the list of all others which meet this criteria.

question from:https://stackoverflow.com/questions/65646016/how-to-find-sum-of-different-combinations-of-all-products-in-list-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you already have your list combo, then you should be able to do the following:

import itertools

sum([i*j for i,j in list(itertools.combinations(combo, 2))])

If combo = [1, 2, 3], then the above code is doing the following:

(1 * 2) + (1 * 3) + (2 * 3)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...