python - 获取python嵌套字典中子值的平均值
问题描述
我有一个表(从 excel 导入)机智要求和子要求的情况,可以看到像这个例子:
+-------------+--------+
| Requeriment | Points |
+-------------+--------+
| 1 | |
+-------------+--------+
| 2 | |
+-------------+--------+
| 3 | |
+-------------+--------+
| 3.1 | |
+-------------+--------+
| 3.2 | |
+-------------+--------+
| 3.3 | |
+-------------+--------+
| 4 | |
+-------------+--------+
| 5 | |
+-------------+--------+
| 5.1 | |
+-------------+--------+
| 5.2 | |
+-------------+--------+
| 5.2.1 | |
+-------------+--------+
| 5.2.1.1 | |
+-------------+--------+
| 5.3 | |
+-------------+--------+
| 6 | |
+-------------+--------+
所有需求最大值为'1.0',如果存在需求有子需求的情况,需求值=总和(子需求)/总子需求,它也适用于具有子需求的子需求。
在这里,我展示了如何评估没有子需求的需求,如果它不完整,则评估为 0,如果已完成,则评估为 1.0。
具有子需求的需求将使用公式进行评估:需求点=子需求的总和/子需求的值。值将是这样的:
该表在字典中转换如下:
{requeriments:{
'1':{'points':'', subrequeriments: {}},
'2':{'points':'', subrequeriments: {}},
'3':{'points':'', subrequeriments: {
'3.1':{'points':'', subrequeriments: {} },
'3.2':{'points':'', subrequeriments: {} },
'3.3':{'points':'', subrequeriments: {} }
}
},
'4':{'points':'', subrequeriments: {}},
'5':{'points':'', subrequeriments: {
'5.1':{'points':'', subrequeriments: {} },
'5.2':{'points':'', subrequeriments: {
'5.2.1':{'points':'', subrequeriments: {
'5.2.1.1':{'points':'', subrequeriments: {}
}
}
},
'5.3':{'points':'', subrequeriments: {} }
}
},
'6':{'points':'', subrequeriments: {}}
}
}
最后,我无法找到一个好方法来评估在嵌套子需求中应用函数需求值 = sum(subrequirments)/total subrequirments 的需求。
您是否知道将其应用于字典的好方法,或者至少将其应用于丢失或桌子的好方法?
解决方案
使用递归函数:
def calcPoints(d):
points = 0
for k,v in d.items():
if v['points'] == '':
v['points'] = calcPoints(v['subrequirements'])
points += float(v['points'])
return str(points / max(1,len(d)))
输入:
data = {'requeriments':
{'1': {'points': '1', 'subrequirements': {}},
'2': {'points': '0', 'subrequirements': {}},
'3': {'points': '', 'subrequirements':
{'3.1': {'points': '1', 'subrequirements': {}},
'3.2': {'points': '0', 'subrequirements': {}},
'3.3': {'points': '1', 'subrequirements': {}}}},
'4': {'points': '1', 'subrequirements': {}},
'5': {'points': '', 'subrequirements':
{'5.1': {'points': '1', 'subrequirements': {}},
'5.2': {'points': '', 'subrequirements':
{'5.2.1': {'points': '', 'subrequirements':
{'5.2.1.1': {'points': '1', 'subrequirements': {}},
'5.2.1.2': {'points': '0', 'subrequirements': {}}}},
'5.2.2': {'points': '0', 'subrequirements': {}}}},
'5.3': {'points': '1', 'subrequirements': {}}}},
'6': {'points': '0', 'subrequirements': {}}}}
输出:
calcPoints(data['requeriments']) # '0.5694444444444444'
print(data)
{'requeriments':
{'1': {'points': '1', 'subrequirements': {}},
'2': {'points': '0', 'subrequirements': {}},
'3': {'points': '0.6666666666666666', 'subrequirements':
{'3.1': {'points': '1', 'subrequirements': {}},
'3.2': {'points': '0', 'subrequirements': {}},
'3.3': {'points': '1', 'subrequirements': {}}}},
'4': {'points': '1', 'subrequirements': {}},
'5': {'points': '0.75', 'subrequirements':
{'5.1': {'points': '1', 'subrequirements': {}},
'5.2': {'points': '0.25', 'subrequirements':
{'5.2.1': {'points': '0.5', 'subrequirements':
{'5.2.1.1': {'points': '1', 'subrequirements': {}},
'5.2.1.2': {'points': '0', 'subrequirements': {}}}},
'5.2.2': {'points': '0', 'subrequirements': {}}}},
'5.3': {'points': '1', 'subrequirements': {}}}},
'6': {'points': '0', 'subrequirements': {}}}}
推荐阅读
- antlr4 - ANTLR4 中用于分析数据类型和变量描述的识别器语法错误
- laravel - 删除laravel中的所有存储文件
- assembly - 当我尝试 DIV 时,我得到“除以零”但它不是零
- string - 如何在kotlin中从旧字符串创建新字符串
- java - 使用上下文路径的 POST 调用在 Spring Boot 2.x 中不起作用
- c - 客户端读取的字节数多于在 C 中发送的字节数 - linux
- c++ - 如何从其他线程中断 std::cin(std::cin.getline) 执行
- git - vs代码在打开核心编辑器后不会停止在 git rebase -i 中间步骤
- python - 使用 pip 安装 PyTorch 时出错 - 无法构建轮子
- azure - 如何更改 Windows 容器的 webapp 服务中的时区