python - 将字典列表转换为唯一的字典列表

标签 python parsing dictionary python-2.7

我一直在努力解决以下问题,但没有成功。

我有一个如下所示的数据结构:

[   {   'ROOT': [   
            {   'firstElem': 'gc-3/1/0'},
            {   'SecondElem': '5.0.0.1'},
            {   'ThirdElem': '127.3.15.1'},
            {   'index': 16},
            {   'function': 'session'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]},
    {   'ROOT': [
            {   'firstElem': 'gc-4/1/0'},
            {   'SecondElem': '5.0.0.2'},
            {   'ThirdElem': '127.3.4.1'},
            {   'index': 5},
            {   'function': 'stand'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]}
]

我想遍历这个数据结构并组合所有具有相同名称的字典元素并创建一个列表。 这很难解释,我已经创建了一个我正在寻找的示例结构:

{
    "ROOT": [
        {
            "firstElem": "gc-3/1/0", 
            "SecondElem": "5.0.0.1", 
            "ThirdElem": "128.0.2.19", 
            "index": "13", 
            "function": "session", 
            "hw": "1.11.0.0 ", 
            "sw": "1.50.0.228 ", 
            "resources": {
                "cpu-info": {
                    "cpu-peak-load": "1", 
                    "cpu-avg-load": "1",
                }, 
                "memory-total": "1", 
                "memory-used": "2", 
            }, 
        }, 
        {
            "firstElem": "gc-4/1/0", 
            "SecondElem": "5.0.0.1", 
            "ThirdElem": "128.0.2.19", 
            "index": "13", 
            "function": "session", 
            "hw": "1.11.0.0 ", 
            "sw": "1.50.0.228 ", 
            "resources": {
                "cpu-info": {
                    "cpu-peak-load": "8", 
                    "cpu-avg-load": "1", 
                }, 
                "memory-total": "1", 
                "memory-used": "2", 
            },  
        }
    ], 
}

我坚持使用原始数据结构并且无法更改它。任何帮助表示赞赏。 上面提供的结构只是一个例子,因为数据是动态接收的,所以我不知道标签名称。所以请不要提供使用特定标签名称的解决方案。

最佳答案

这里有一个方法:

>>> from collections import defaultdict
>>> def  combine(item):
    # Easy return if not a list: element itself
    if type(item) != type([]):
        return item
    # else call recursion
    first_ret = [(i.items()[0][0], combine(i.items()[0][1])) for i in item]

    # Here we group by same keys if any ('ROOT', for instance)
    count_keys = defaultdict(list)
    for couple in first_ret:
        count_keys[couple[0]].append(couple[1])
    return dict((k, v if len(v) > 1 else v[0]) for k, v in count_keys.iteritems())

我必须对 ROOT 节点进行分组,但它似乎可以正常工作:

>>> pprint(combine(l))
{'ROOT': [{'SecondElem': '5.0.0.1',
           'ThirdElem': '127.3.15.1',
           'firstElem': 'gc-3/1/0',
           'function': 'session',
           'hw': '0.0.0.0',
           'index': 16,
           'resources': {'cpu-info': {'cpu-avg-load': 1,
                                      'cpu-peak-load': 1},
                         'memory-total': 1,
                         'memory-used': 2},
           'sw': '1.50.1.3'},
          {'SecondElem': '5.0.0.2',
           'ThirdElem': '127.3.4.1',
           'firstElem': 'gc-4/1/0',
           'function': 'stand',
           'hw': '0.0.0.0',
           'index': 5,
           'resources': {'cpu-info': {'cpu-avg-load': 1,
                                      'cpu-peak-load': 1},
                         'memory-total': 1,
                         'memory-used': 2},
           'sw': '1.50.1.3'}]}
>>> 

关于python - 将字典列表转换为唯一的字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14837231/

相关文章:

algorithm - 实用 Earley 解析 (Aycock & Horspool 2002) : How to add back pointers?

javascript - 关闭莫里斯图上的日期解析

java - 使用 Wea​​kHashMaps 模拟 DELETE 级联

c# - 将项目添加到字典中的列表

list - 如何更新列表的一个元素

python - 渲染到模板并获得响应

python - Python 中的新运算符

for循环中列表修改的python行为

python - Tkinter Canvas 失效问题(对象在移动时被剪裁)

C# DateTime 解析短字符串 ("MMMyy")