Python 我如何计算一个列表的总数
我正在搜索如何创建包含6个数字的列表。 我有一个我研究过的数字,比如30,是我名单上数字相加的结果 这里有一个简单的例子:Python 我如何计算一个列表的总数,python,list,function,Python,List,Function,我正在搜索如何创建包含6个数字的列表。 我有一个我研究过的数字,比如30,是我名单上数字相加的结果 这里有一个简单的例子: list=[] list.append[1,5,5,10,8,2] and the number that I want in rapport with the list is for example 30 所以解是5+5+10+8+2=30 它返回30步的不同步骤 当然你有一个限制。这是因为你不能使用相同的数字2次,但你可以储存一个结果,以便以后使用 目前,我只有一个
list=[]
list.append[1,5,5,10,8,2]
and the number that I want in rapport with the list is for example 30
所以解是5+5+10+8+2=30
它返回30步的不同步骤
当然你有一个限制。这是因为你不能使用相同的数字2次,但你可以储存一个结果,以便以后使用
目前,我只有一个函数返回列表的不同加法,但我无法将结果与下一个数字相加
所以我的问题是,我如何创建一个函数,用我列表中的数字来尝试所有可能的加法,例如这里的30
例如,我想以我的上述清单为例:
1+5=6 # 30 so I continue, 6+5#11 so I continue ... And at the end I need find the right way to find 30
因此,这里的解决方案是:
5+5=10
10+10=20
20+8=28
28+2=30 and 30 is my research number so we stop the function and we print the steps to have the good solution.
谢谢 我认为这是一个经典问题(我不记得名称),我为您编写了一个简单的基于回溯的解决方案。应该有更好的(性能方面的)解决方案 你可以把答案放在一个全局列表中,或者通过一些工作返回,而不是打印
def sub(all_numbers: list, current_index: int, goal: int) -> bool:
"""
Tries to construct goal using all_numbers[current_index:] if the goal can be reached it will print picked numbers
and return True
"""
if current_index >= len(all_numbers):
return goal == 0
if goal < 0:
return False
current = all_numbers[current_index]
pick_current_result = sub(all_numbers, current_index + 1, goal - current)
if pick_current_result:
print(current)
return True
dont_pick_current_result = sub(all_numbers, current_index + 1, goal)
return dont_pick_current_result
def solve(all_numbers: list, goal: int):
sub(all_numbers, 0, goal)
solve([1, 5, 5, 10, 8, 2], 30)
def sub(所有编号:列表,当前索引:int,目标:int)->bool:
"""
尝试使用所有_编号[当前_索引:]构建目标如果目标可以实现,它将打印选中的编号
并返回True
"""
如果当前索引>=len(所有编号):
返回目标==0
如果目标<0:
返回错误
当前=所有编号[当前索引]
选择当前结果=子项(所有数字,当前索引+1,目标-当前)
如果选择当前结果:
打印(当前)
返回真值
不选择当前结果=子(所有数字,当前索引+1,目标)
返回不拾取当前结果
def solve(所有数字:列表,目标:整数):
sub(所有_编号,0,目标)
求解([1,5,5,10,8,2],30)
以下是一种使用递归生成器函数的方法:
def sumto(n, lst):
if not n and not lst: # base case 1: empty list = 0
yield []
return
if n < 0 or not lst: # base case 2: unsolvable
return
head, *tail = lst
for sol in sumto(n-head, tail): # recursion 1: use first element
yield [head] + sol
yield from sumto(n, tail) # recursion 2: don't use first element
>>> list(sumto(30, [1,5,5,10,8,2]))
[[5, 5, 10, 8, 2]]
>>> list(sumto(28, [1,5,5,10,8,2]))
[[5, 5, 10, 8]]
>>> list(sumto(42, [1,5,5,10,8,2]))
[]
>>> list(sumto(10, [1,5,5,10,8,2]))
[[5, 5], [10], [8, 2]]
def sumto(n,lst):
如果不是n且不是lst:#基本情况1:空列表=0
收益率[]
返回
如果n<0或不是lst:#基本情况2:无法解决
返回
头部,*尾部=lst
对于sumto中的sol(n-head,tail):#递归1:使用第一个元素
收益率[水头]+sol
sumto(n,tail)#递归2:不使用第一个元素
>>>清单(sumto(30[1,5,5,10,8,2]))
[[5, 5, 10, 8, 2]]
>>>清单(sumto(28[1,5,5,10,8,2]))
[[5, 5, 10, 8]]
>>>清单(sumto(42[1,5,5,10,8,2]))
[]
>>>清单(sumto(10[1,5,5,10,8,2]))
[[5, 5], [10], [8, 2]]
你的问题是什么?我将我的帖子改为更清楚地解释我的问题你是在问:我如何从我的列表中获得所有可能的数字组合,并对每个组合进行汇总?是的,我的列表中所有可能的组合,但我需要一个特定的约束。我编辑我的帖子是为了向你展示更多。恐怕你的问题非常不清楚。我不明白你在问什么。也许其他人可以帮你解决问题。@raph目标是你试图找到的总数。一开始是30。在下一步中,我们假设取1并在列表的其余部分([5,5,10,8,2]
)中搜索29。什么是*tail?tail==lst[1://code>。符号a,*b=c
将iterablec
解包,将其第一个元素分配给a
,并将其剩余部分打包到listb
中。因此,以第一个元素为例,将第一个元素与列表中的所有其他元素一起计算?@raph您可以,最好是一条注释。我可以在帖子中添加解释。你看到我的问题了吗?