python - 为什么我的递归会导致自引用 dict 值?
问题描述
我的目标是编写一个函数,通过根据收益/成本比比较选项并将它们存储在嵌套字典列表中,从而在选项之间拆分预算。当具有相同收益/成本比的多个选项可用时,每个选项都应单独进行(作为下游元素,而下游元素又可能具有多个下游元素)并反映为其上游字典的字典列表。对于可能出现的选项数量没有限制。
def get_all_allocation_proposals(budget, options, upstream_element=dict()):
# accepts:
# budget to be allocated
# list of options
# returns:
# a list of allocation proposals
# filter options for affordable options and sort by cost benefit ratio
options = [x for x in options if x['cost'] <= budget]
options = sorted(options, key=lambda x: (
x['benefit_cost_ratio'], x['benefit']), reverse=True)
if (len(options) > 0):
# select the best options
best_bc_ratio = options[0]['benefit_cost_ratio']
best_options = [
x for x in options if x['benefit_cost_ratio'] == best_bc_ratio]
upstream_element['downstream_elements'] = []
for current_element in best_options:
downstream_options = remove_conflicting_options(
current_element, options)
downstream_budget = budget - current_element['cost']
current_element['donstream_budget'] = downstream_budget
downstream_elements = get_all_allocation_proposals(downstream_budget,
downstream_options,
current_element)
if downstream_elements is not None:
current_element['downstream_elements'] = downstream_elements
upstream_element['downstream_elements'].append(current_element)
return upstream_element
else:
return None
在上面的代码中,当添加元素时,会创建自引用 dict 值。为什么会这样,我该如何避免呢?我要做的就是将所有下游元素传递给第一个调用堆栈。我的递归模式是否存在根本缺陷?
解决方案
我认为问题可能是因为您将可变对象传递到递归调用中。具体来说downstream_options
,current_element
是 dicts,当你在函数的给定递归中修改它们时,你也在上面的级别修改它们,在这种情况下,这似乎让你试图在 dict 中为自身分配一个值(或者一些这样的不可能,我还没有完全遵循逻辑)。
一个快速的解决方案可能是(我不确定这是否会破坏你的逻辑)在每次递归时制作这些字典的副本:
from copy import deepcopy
...
downstream_elements = get_all_allocation_proposals(downstream_budget,
deepcopy(downstream_options),
deepcopy(current_element))
此外,如评论中所述,您应该避免使用可变的默认参数,即upstream_element=dict()
. 如果您实际使用默认值(您的代码中没有出现),这可能会产生一些非常令人困惑的行为
推荐阅读
- reactjs - 与 mapStateToProps 一起使用连接时,“无法读取未定义的属性“地图”
- wildfly - keycloak : 太多的重定向,记得我检查
- php - PHP - $_FILES 没有出现
- swift - 端口扬声器(类型:扬声器)的未知选定数据源?
- java - Spring引导多个jndi连接
- visual-studio-code - 一次性安装 VS Code 扩展
- mongodb - 打开授权后,MongoDB 未在 Ubuntu 16.04 中启动
- dart - 带有 CircularProgressIndicator 和 GridView 小部件的 Flutter Stack,不能正常工作
- oauth - 使用 google Oauth 需要密钥
- android-source - 构建aosp时为键盘添加两种语言