我一直在努力奋斗几个小时,以解决以下问题,但未取得成功。将字典列表转换为唯一的字典列表
我有一个数据结构,看起来像这样:
[ { '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",
},
}
],
}
我坚持与原始数据结构和不能改变它。任何帮助表示赞赏。 上面提供的结构只是一个例子,因为数据是动态接收的,我不会知道标签名称。所以请不要提供使用特定标签名称的解决方案。
用真实代码测试,它不是工作,它似乎只采取一个根元素,第二个不知何故被丢弃! – theAlse 2013-02-13 09:28:21
@theAlse:在某处发布实际数据(例如gist.github.com) – georg 2013-02-13 09:46:37