elegant way to reduce a list of dictionaries?

前端 未结 5 1209
我在风中等你
我在风中等你 2021-01-13 02:30

I have a list of dictionaries and each dictionary contains exactly the same keys. I want to find the average value for each key and I would like to know how to do it using r

5条回答
  •  清酒与你
    2021-01-13 03:25

    As an alternative, if you're going to be doing such calculations on data, then you may wish to use pandas (which will be overkill for a one off, but will greatly simplify such tasks...)

    import pandas as pd
    
    data = [
      {
        "accuracy": 0.78,
        "f_measure": 0.8169374016795885,
        "precision": 0.8192088044235794,
        "recall": 0.8172222222222223
      },
      {
        "accuracy": 0.77,
        "f_measure": 0.8159133315763016,
        "precision": 0.8174754717495807,
        "recall": 0.8161111111111111
      },
      {
        "accuracy": 0.82,
        "f_measure": 0.8226353934130455,
        "precision": 0.8238175920455686,
        "recall": 0.8227777777777778
      }, # ...
    ]
    
    result = pd.DataFrame.from_records(data).mean().to_dict()
    

    Which gives you:

    {'accuracy': 0.79000000000000004,
     'f_measure': 0.8184953755563118,
     'precision': 0.82016728940624295,
     'recall': 0.81870370370370382}
    

提交回复
热议问题