获取 python 嵌套字典中子值的平均值

Get average of sub values in python nested dictionary

我有一个 table(从 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/value 的总和。该值将是这样的:

这个 table 在字典中被转换成这样:

{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: {}}
 }
}

最后我找不到一个很好的方法来评估嵌套子需求中应用函数需求值=总和(子需求)/总子需求的需求。

您知道将其应用于字典的好方法,或者至少知道将其应用于迷路或 table 的好方法吗?

使用递归函数:

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': {}}}}