【问题标题】:Aggregate certain values in array of dictionary based on key/value criteria根据键/值标准聚合字典数组中的某些值
【发布时间】:2020-03-30 08:59:57
【问题描述】:

我有以下论坛帖子的 JSON。 创建每个论坛汇总的正面/负面评级的结果 JSON 的 pythonic 方式是什么?

输入Json:

{"Posting_Stats":{
      "Posts":[
         {
            "Date":"2020-03-29 12:41:00",
            "Forum":"panorama",
            "Positive":2,
            "Negative":0
         },
         {
            "Date":"2020-03-29 12:37:00",
            "Forum":"web",
            "Positive":6,
            "Negative":0
         },
         {
            "Date":"2020-03-29 12:37:00",
            "Forum":"web",
            "Positive":2,
            "Negative":2
         },...]}

输出应该是:

{"Forum_Stats" : [{"Forum" : "panorama",
                  "Positive":2,
                  "Negative":0},
                 {"Forum" : "web",
                  "Positive":8,
                  "Negative":2},...]
}

]

【问题讨论】:

    标签: python arrays json list dictionary


    【解决方案1】:

    想不出别的办法:

    posts = inputData['Posting_Stats']['Posts']
    postAggregator = {}
    for post in posts:
        try:
            postAggregator[post['Forum']]['Positive'] += post.get('Positive',0)
            postAggregator[post['Forum']]['Negative'] += post.get('Negative',0)
        except KeyError:
            postAggregator.update({post['Forum']:{"Positive":post.get('Positive',0), "Negative":post.get('Negative',0)}})
    
    outputData = {"Forum_Stats": []}
    for key, value in postAggregator.items():
        outputData['Forum_Stats'].append({"Forum":key , "Positive":value['Positive'],"Negative":value['Negative']})
    
    print(outputData)
    

    输出:

    {'Forum_Stats': [{'Forum': 'panorama', 'Positive': 2, 'Negative': 0}, {'Forum': 'web', 'Positive': 8, 'Negative': 2}]}
    

    【讨论】:

      【解决方案2】:

      这可能是一种解决方法:

      #taking the input in a dictionary
      d = {"Posting_Stats":{
            "Posts":[
               {
                  "Date":"2020-03-29 12:41:00",
                  "Forum":"panorama",
                  "Positive":2,
                  "Negative":0
               },
               {
                  "Date":"2020-03-29 12:37:00",
                  "Forum":"web",
                  "Positive":6,
                  "Negative":0
               },
               {
                  "Date":"2020-03-29 12:37:00",
                  "Forum":"web",
                  "Positive":2,
                  "Negative":2
               }]}}
      
      #iterating over the values to get their some on the basis of forum as key
      temp = {}
      for i in d.get('Posting_Stats').get('Posts'):
          if temp.get(i.get('Forum')) == None:
              temp[i.get('Forum')] = {}
              temp[i.get('Forum')]['Positive'] = 0
              temp[i.get('Forum')]['Negative'] = 0
          temp[i.get('Forum')]['Positive']+=i.get('Positive')
          temp[i.get('Forum')]['Negative']+=i.get('Negative')
      

      最后将输出转换成需要的格式

      output = [{'Forum': i , **temp[i] } for i in temp]
      print(output)
      
      #[{'Forum': 'panorama', 'Positive': 2, 'Negative': 0},
      #{'Forum': 'web', 'Positive': 8, 'Negative': 2}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 2020-09-27
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多