简体   繁体   English

如何查找列表中所有产品的不同组合的总和(python)

[英]How to find sum of different combinations of all products in list (python)

I have the following two python lists of numbers:我有以下两个 python 数字列表:

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, eg然后我有一个 class 在这里我创建了一个方法,它只是两个列表中给定元素的乘积的正负(取决于其他 class 变量),例如

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.我想获得所有可能的产品组合的 class 实例的总和。 These can be repeated (ie (2, 20) can be used more than once, and so on) so the amount of combinations will be very large.这些可以重复(即 (2, 20) 可以多次使用,依此类推),因此组合的数量会非常大。

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.但这太长了,甚至无法打印,更不用说通过方程式传递 this 的每个元素的问题了。

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 (ie some form of ML).我认为对于简单的 for 循环/函数而言,这可能是一项太大的任务,并且可以使用一些更复杂的方法(即某种形式的 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编辑:一个最小的可重现示例如下:如果每个第二个实例都是一个负积,考虑到元组可以重复,一个例子是如果使用 bet1 (2, 20) 和 bet2 - bet100 使用 (2, 5) (所以 99 次)那么 pos_combos 列表中的一个条目将是元组的元组

[((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.因为它们的总和大于 0,因为它等于 40。我想获取所有其他符合此标准的列表。

If you already have your list combo , then you should be able to do the following:如果您已经有了列表combo ,那么您应该能够执行以下操作:

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:如果combo = [1, 2, 3] ,则上面的代码执行以下操作:

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

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

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