如何选择列表的值并将其添加到Python中以获得特定的值?

如何选择列表的值并将其添加到Python中以获得特定的值?,python,Python,我有以下清单: list_1=[5,10] 列表_2=[50] 清单3=[5,10,15] 我需要得到一个特定的值(本例中为60),选择每个列表中的一个元素maximum 例如: 我可以得到60加10(值来自list\u1)+50(list\u2),或者加10(值来自list\u3)+50(list\u2),或者加5(list\u1)+50(list\u2)+5(list\u3) 我怎样才能做到这一点?有任何函数吗?您可以使用优秀模块中的和函数,通过简单的暴力方法来实现这一点 如您所见,ite

我有以下清单:

list_1=[5,10]
列表_2=[50]
清单3=[5,10,15]
我需要得到一个特定的值(本例中为60),选择每个列表中的一个元素maximum

例如:

我可以得到60加10(值来自
list\u1
)+50(
list\u2
),或者加10(值来自
list\u3
)+50(
list\u2
),或者加5(
list\u1
)+50(
list\u2
)+5(
list\u3


我怎样才能做到这一点?有任何函数吗?

您可以使用优秀模块中的和函数,通过简单的暴力方法来实现这一点

如您所见,
itertools.combinations()
位于
for
循环中,因此它被调用了3次,以给出长度为1、2和3的组合

然后将这些组合传递到
itertools.product(*lists)
,它只从每个列表(iterable)中获取一项

导入itertools
清单1=[5,10]
列表_2=[50]
清单3=[5,10,15]
比值=60
对于范围内的长度(1,3+1):
列表组合=itertools.组合((列表1、列表2、列表3)、长度)
对于列表组合中的列表:
打印(f“长度为{length}:{lists}的列表组合中的当前列表”)
对于itertools.product(*列表)中的ntuple:
求和=求和(N倍)
等式=“+”.join(映射(str,ntuple))
match=“[match!]”如果求和==特定值,则为else“”
打印(f“{等式}={求和}{匹配}”)
打印()
输出:

Currect lists from combination of lists of length 1: ([5, 10],)
5 = 5
10 = 10

Currect lists from combination of lists of length 1: ([50],)
50 = 50

Currect lists from combination of lists of length 1: ([5, 10, 15],)
5 = 5
10 = 10
15 = 15

Currect lists from combination of lists of length 2: ([5, 10], [50])
5 + 50 = 55
10 + 50 = 60 [Match!]

Currect lists from combination of lists of length 2: ([5, 10], [5, 10, 15])
5 + 5 = 10
5 + 10 = 15
5 + 15 = 20
10 + 5 = 15
10 + 10 = 20
10 + 15 = 25

Currect lists from combination of lists of length 2: ([50], [5, 10, 15])
50 + 5 = 55
50 + 10 = 60 [Match!]
50 + 15 = 65

Currect lists from combination of lists of length 3: ([5, 10], [50], [5, 10, 15])
5 + 50 + 5 = 60 [Match!]
5 + 50 + 10 = 65
5 + 50 + 15 = 70
10 + 50 + 5 = 65
10 + 50 + 10 = 70
10 + 50 + 15 = 75

到底是什么问题?你尝试过什么,做过什么研究吗?只是出于好奇,你想解决的根本问题是什么?或者这仅仅是一个谜?