几个小时来,我一直在为解决以下问题而奋斗,但没有成功。
我有一个数据结构,如下所示:
[ { '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}]},
]}
]我想遍历这个数据结构,用相同的名称组合所有的dict元素,然后创建一个列表。这一点很难解释,我创建了一个示例结构来说明我想要的是什么:
{
"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",
},
}
],
}我被原来的数据结构困住了,不能改变它,。任何帮助都是非常感谢的。上面提供的结构只是一个例子,因为数据是动态接收的,所以我将不知道标记名。因此,请不要提供使用特定标签名称的解决方案。
发布于 2013-02-12 17:41:01
以下是一种方法:
>>> 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'}]}
>>> 发布于 2013-02-12 16:54:20
让我们试试这个:
r = {}
def lst2dct(lst):
return (lst if not isinstance(lst, list) else
{k: lst2dct(v) for e in lst for k, v in e.items()})
for e in source:
key, val = e.items()[0]
r.setdefault(key, []).append(lst2dct(val))发布于 2013-02-12 16:58:36
有点烦人,但你可以试试:
data = [ { '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_list = [
]
final_data = {
'ROOT' : root_list
}
for dict in data:
if dict['ROOT'] not in final_data['ROOT']:
final_data['ROOT'].append(dict['ROOT'])https://stackoverflow.com/questions/14837231
复制相似问题