python - 将 JSON 对象转换为相似键的 JSON 对象数组

标签 python

嘿,我的输入是 JSON 对象列表

[ 
{ 
  ‘sr_number’:687485784,
        ‘IC_Prediction’:’NPU-Soldier-Fractures’,
        ’ IC_Probability’:0.80,'Signatures':'PLATFORM-MLP-2'
     },
     { 
        'sr_number’:687485784,
        ‘IC_Prediction’:’Hotswap-Controller,
        ’IC_Probability’:0.63,
        'Signatures' : 'PLATFORM-NLP-3'
     }
  },{},{},{} ]

我希望根据“sr_number”对它们进行分组,期望输出如下。

[ { ‘sr_number’ : 687485784,‘recommedations’: 
[{‘IC_Prediction’:’NPU-Soldier-Fractures’,’IC_Probability’:0.80},
{‘IC_Prediction’:’Hotswap-Controller,’ IC_Probability’:0.63}
],
‘Signatures’: [ ‘PLATFORM-MLP-2’,’PLATFORM-NLP-3’]
},{},{}
]

我是Python新手,所以不知道要使用哪种数据结构以及如何完成它。任何帮助将不胜感激。

最佳答案

尝试下面的代码:

data = [{'IC_Prediction': 'NPU-Soldier-Fractures', 'IC_Probability': 0.8, 'Signatures': 'PLATFORM-MLP-2', 'sr_number': 687485784}, {'IC_Prediction': 'Hotswap-Controller', 'IC_Probability': 0.63, 'Signatures': 'PLATFORM-NLP-3', 'sr_number': 687485784}, {}, {}, {}]

inter_dict = dict()
for row in data:
    sr_number = row.get('sr_number',"")
    if not sr_number:
        continue
    if sr_number and inter_dict.get(sr_number):
        _dict_new = inter_dict.get(sr_number)
        if _dict_new.get('recommedations', ""):
            arr = _dict_new.get('recommedations')
            arr.append({
                        'IC_Probability': row.get('IC_Probability'),
                        'IC_Prediction': row.get('IC_Prediction') 
            })
            _dict_new.update({'recommedations': arr})
        else:
            _dict_new.update({'recommedations': [{
                                                'IC_Probability': row.get('IC_Probability'),
                                                'IC_Prediction': row.get('IC_Prediction') 
                                                }]})

        if _dict_new.get('Signatures',""):
            arr = _dict_new.get('Signatures',"")
            arr.append(row.get('Signatures'))
            _dict_new.update({'Signatures': arr})
        else:
            _dict_new.update({'Signatures': [row.get('Signatures')]})
    else:
        inter_dict.update({sr_number: 
                            {
                                'sr_number': sr_number, 
                                'recommedations': [{
                                                    'IC_Probability': row.get('IC_Probability'),
                                                    'IC_Prediction': row.get('IC_Prediction')
                                                  }],
                                'Signatures': [row.get('Signatures')]

                            }

                        })


print([v for k, v in inter_dict.items()])
"""
[{'sr_number': 687485784, 'Signatures': ['PLATFORM-MLP-2', 'PLATFORM-NLP-3'], 'recommedations': [{'IC_Probability': 0.8,
 'IC_Prediction': 'NPU-Soldier-Fractures'}, {'IC_Probability': 0.63, 'IC_Prediction': 'Hotswap-Controller'}]}]  
"""

关于python - 将 JSON 对象转换为相似键的 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59391459/

相关文章:

python - 如何使用 pytest 确认引发正确的异常

python - ply 中的特殊情况词法分析器规则

python - Django:从那时起改变语言

python - 根据每个列表的子集从列表列表中删除重复项

python - jedi-vim 是否与 YouCompleteMe 冲突?

python - 向量中的元素范围?

python - 使用 python asyncio 从套接字读取时如何避免阻塞?

python - Django - 类内属性的上下文字典

python - 使用 python 的聚类图可视化

python - Django 模板以 "items"为键遍历 dictionary.items