首页 > 解决方案 > 获取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 的需求。

您是否知道将其应用于字典的好方法,或者至少将其应用于丢失或桌子的好方法?

标签: pythonlistdictionarynestedevaluation

解决方案


使用递归函数:

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

推荐阅读