Python如何更新嵌套字典中指定键值连接对应的概率值
问题描述
现有两层嵌套结构的Python字典:
- 外层键为一级节点ID
- 内层键为二级节点ID
- 内层键对应的值为列表,列表内存储若干元组,元组第一项为连接名称,第二项为该连接对应的概率值
字典结构示例如下:
{ 25255942: { 52691892: [("Internet of Things (IOT) Device Management", prob1)], 72359602: [ ("Internet of Things (IOT) Device Management", prob2), ("Questions", prob3), ], }, 185589766: { 183701781: [ ('"Cloud Computing" How to use it for your business', prob4), ("Learn How to Be a Success in Network Marketing", prob5), ], 183702935: [ ('"Cloud Computing" How to use it for your business', prob6), ("Learn How to Be a Success in Network Marketing", prob7), ], 110069642: [ ("Learn How to Be a Success in Network Marketing", prob8), ("How to make money in network marketing", prob9), ], }, }
需要实现更新指定「外层键、内层键、连接名称」组合对应概率值的功能,原有实现代码无法完成修改,代码如下:
def modify_sign_a(v, x, connection, dictionary, value): for node in dictionary: if node == v: for follower in dictionary[node]: if follower == x for follower_info in dictionary[node][follower]: if follower_info[0] == connection: follower_info = list(follower_info) follower_info[1] = follower_info[1] + value follower_info = tuple(follower_info)
提供部分测试字典数据如下:
{25255942: {52691892: [('Internet of Things (IOT) Device Management', 0)], 72359602: [('Internet of Things (IOT) Device Management', 0), ('Questions', 0)]}, 185589766: {183701781: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 183702935: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 110069642: [('Learn How to Be a Success in Network Marketing', 0), ('How to make money in network marketing', 0)]}, 110370832: {9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Afterwork Happy Hours and Dinner', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0)], 9779381: [("Let's get a Furmeet setup and make a home here", 0)]}, 86319132: {101605182: [('Line Dancing to All Kinds of Music', 0), ('Why We Need Highly Sensitive People', 0), ("Meeting other SAHM's", 0), ('Seminars on First Time Home Buyers', 0)], 97636712: [('Public Speaking as a Means to Market your Business', 0), ('Where or where to go on a honeymoon?', 0), ('Reading and discussion', 0)]}, 40681502: {9024523: [('Activities', 0), ('Dinner and a Movie', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 40323812: [('Activities', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0), ('Dining Out, BBQs, Food Fairs, Happy Hour and More', 0)], 12747253: [('Activities', 0), ("Let's get a Furmeet setup and make a home here", 0)], 110370832: [('Dinner and a Movie', 0), ('How Do I Begin My Home Search', 0)], 9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 123979682: [('Dinner and a Movie', 0), ('Healthy Cooking For You and Your Family', 0), ('"Cloud Computing" How to use it for your business', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('"Cloud Computing" How to use it for your business', 0)], 13949877: [("Let's get a Furmeet setup and make a home here", 0), ('How Do I Begin My Home Search', 0), ('Learn How to Stop Foreclosure', 0)]}, 47390772: {96547882: [('Business start up', 0), ('How to start a business', 0), ('start a business', 0)], 60915162: [('Business start up', 0)], 12480975: [('Business start up', 0)], 80749802: [('Business start up', 0)]}, 12017724: {3805969: [('Learn How to Be a Success in Network Marketing', 0), ('What to look for when purchasing a new home', 0)]}}
要求实现正确的修改逻辑,并验证是否可以将三元组<25255942,72359602,'Questions'>对应的概率值从0修改为1。
原有代码失效原因
- 语法错误:
if follower == x行末尾缺少冒号,代码无法正常运行 - 逻辑错误:遍历元组时拿到的
follower_info是临时循环变量,将其转为列表修改后再转回元组的操作,仅修改了临时变量的值,没有将新元组写回原列表,原字典存储的内容不会发生任何变化 - 效率问题:逐层遍历所有字典键做相等判断的逻辑冗余,Python字典支持直接按键取值,时间复杂度更低
正确实现代码
def modify_sign_a(outer_key, inner_key, target_connection, data, add_value): # 校验目标路径是否存在,避免触发KeyError if outer_key not in data or inner_key not in data[outer_key]: return False conn_list = data[outer_key][inner_key] # 带索引遍历连接列表,直接替换列表内对应位置的元组 for idx, (conn_name, prob) in enumerate(conn_list): if conn_name == target_connection: conn_list[idx] = (conn_name, prob + add_value) return True # 未找到匹配的连接名称 return False
功能验证
使用提供的测试数据执行验证,代码如下:
# 加载测试字典 test_data = {25255942: {52691892: [('Internet of Things (IOT) Device Management', 0)], 72359602: [('Internet of Things (IOT) Device Management', 0), ('Questions', 0)]}, 185589766: {183701781: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 183702935: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 110069642: [('Learn How to Be a Success in Network Marketing', 0), ('How to make money in network marketing', 0)]}, 110370832: {9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Afterwork Happy Hours and Dinner', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0)], 9779381: [("Let's get a Furmeet setup and make a home here", 0)]}, 86319132: {101605182: [('Line Dancing to All Kinds of Music', 0), ('Why We Need Highly Sensitive People', 0), ("Meeting other SAHM's", 0), ('Seminars on First Time Home Buyers', 0)], 97636712: [('Public Speaking as a Means to Market your Business', 0), ('Where or where to go on a honeymoon?', 0), ('Reading and discussion', 0)]}, 40681502: {9024523: [('Activities', 0), ('Dinner and a Movie', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 40323812: [('Activities', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0), ('Dining Out, BBQs, Food Fairs, Happy Hour and More', 0)], 12747253: [('Activities', 0), ("Let's get a Furmeet setup and make a home here", 0)], 110370832: [('Dinner and a Movie', 0), ('How Do I Begin My Home Search', 0)], 9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 123979682: [('Dinner and a Movie', 0), ('Healthy Cooking For You and Your Family', 0), ('"Cloud Computing" How to use it for your business', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('"Cloud Computing" How to use it for your business', 0)], 13949877: [("Let's get a Furmeet setup and make a home here", 0), ('How Do I Begin My Home Search', 0), ('Learn How to Stop Foreclosure', 0)]}, 47390772: {96547882: [('Business start up', 0), ('How to start a business', 0), ('start a business', 0)], 60915162: [('Business start up', 0)], 12480975: [('Business start up', 0)], 80749802: [('Business start up', 0)]}, 12017724: {3805969: [('Learn How to Be a Success in Network Marketing', 0), ('What to look for when purchasing a new home', 0)]}} # 打印修改前的目标值 print("修改前72359602节点的连接列表:", test_data[25255942][72359602]) # 执行修改操作:目标三元组概率值加1 exec_result = modify_sign_a(25255942, 72359602, 'Questions', test_data, 1) print("修改操作结果:", "成功" if exec_result else "失败") # 打印修改后的目标值 print("修改后72359602节点的连接列表:", test_data[25255942][72359602])
运行输出如下:
修改前72359602节点的连接列表: [('Internet of Things (IOT) Device Management', 0), ('Questions', 0)] 修改操作结果: 成功 修改后72359602节点的连接列表: [('Internet of Things (IOT) Device Management', 0), ('Questions', 1)]
验证结论:代码可正常将三元组<25255942,72359602,'Questions'>对应的概率值从0修改为1。
内容的提问来源于stack exchange,提问作者Debbie
相关产品推荐
相关产品推荐

