[英]flattened dictionary into nested dictionary of dictionaries of lists
So I can't seem to figure out how to effectively implement this.所以我似乎无法弄清楚如何有效地实现这一点。 I'm looking to nest a flattened dictionary into dictionary of dictionaries of lists, based on specific keys as inputs.我希望基于特定的键作为输入,将一个扁平的字典嵌套到列表字典的字典中。 Trying so desperately to learn拼命地学习
Given that my data looks like this:鉴于我的数据如下所示:
data= [
{
"player": "Kevin Durant",
"team": "Thunder",
"location": "Oklahoma City",
"points": 15
},
{
"player": "Jeremy Lin",
"team": "Lakers",
"location": "Los Angeles",
"points": 22
},
{
"player": "Kobe Bryant",
"team": "Lakers",
"location": "Los Angeles",
"points": 51
},
{
"player": "Blake Griffin",
"team": "Clippers",
"location": "Los Angeles",
"points": 26
}
]
I'd want to return something like this if I give it parameters like reorder(data,['location','team','player']) for an example如果我给它像 reorder(data,['location','team','player']) 这样的参数作为例子,我想返回这样的东西
result={
"Los Angeles": {
"Clippers": {
"Blake Griffin": [
{
"points": 26
}
]
},
"Lakers": {
"Kobe Bryant": [
{
"points": 51
}
],
"Jeremy Lin": [
{
"points": 22
}
]
}
},
"Oklahoma City": {
"Thunder": {
"Kevin Durant": [
{
"points": 15
}
]
}
},
}
You can use the setdefault function to automatically build the nesting levels as you go through the data:您可以使用 setdefault 函数在浏览数据时自动构建嵌套级别:
data= [
{
"player": "Kevin Durant",
"team": "Thunder",
"location": "Oklahoma City",
"points": 15
},
{
"player": "Jeremy Lin",
"team": "Lakers",
"location": "Los Angeles",
"points": 22
},
{
"player": "Kobe Bryant",
"team": "Lakers",
"location": "Los Angeles",
"points": 51
},
{
"player": "Blake Griffin",
"team": "Clippers",
"location": "Los Angeles",
"points": 26
}
]
nested = dict()
for d in data:
nested.setdefault(d["location"],dict()) \
.setdefault(d["team"], dict()) \
.setdefault(d["player"], list()) \
.append({"points":d["points"]})
output:输出:
print(nested)
{ 'Oklahoma City':
{
'Thunder':
{ 'Kevin Durant': [{'points': 15}] }
},
'Los Angeles':
{
'Lakers':
{
'Jeremy Lin': [{'points': 22}],
'Kobe Bryant': [{'points': 51}]
},
'Clippers':
{ 'Blake Griffin': [{'points': 26}] }
}
}
[EDIT] Generalizing the approach [编辑]概括该方法
If you have to do this kind of thing often and on different types of dictionaries or hierarchies, you could generalize it in a function:如果您必须经常在不同类型的字典或层次结构上执行此类操作,则可以将其概括为一个函数:
def dictNesting(data,*levels):
result = dict()
for d in data:
r = result
for level in levels[:-1]:
r = r.setdefault(d[level],dict())
r = r.setdefault(d[levels[-1]],list())
r.append({k:v for k,v in d.items() if k not in levels})
return result
You would then give the function a list of dictionaries followed by the names of the keys you want to nest:然后,您可以为该函数提供一个字典列表,后跟要嵌套的键的名称:
byLocation = dictNesting(data,"location","team")
{ 'Oklahoma City':
{ 'Thunder': [
{'player': 'Kevin Durant', 'points': 15}]
},
'Los Angeles':
{'Lakers': [
{'player': 'Jeremy Lin', 'points': 22},
{'player': 'Kobe Bryant', 'points': 51}],
'Clippers': [
{'player': 'Blake Griffin', 'points': 26}]
}
}
If you want to group the same data in a different way, you just need to change the order of the field names:如果要以不同的方式对相同的数据进行分组,只需更改字段名称的顺序:
byPlayer = dictNesting(data,"player","location","team")
{ 'Kevin Durant':
{ 'Oklahoma City':
{ 'Thunder': [{'points': 15}] }
},
'Jeremy Lin':
{ 'Los Angeles':
{'Lakers': [{'points': 22}]}
},
'Kobe Bryant':
{ 'Los Angeles':
{'Lakers': [{'points': 51}]}
},
'Blake Griffin':
{ 'Los Angeles':
{'Clippers': [{'points': 26}]}
}
}
From there you can have some fun with the function and improve it to aggregate the data at the lowest nesting level:从那里您可以使用该函数并对其进行改进以在最低嵌套级别聚合数据:
def dictNesting(data,*levels,aggregate=False):
result = dict()
for d in data:
r = result
for level in levels[:-1]:
r = r.setdefault(d[level],dict())
r = r.setdefault(d[levels[-1]],[list,dict][aggregate]())
content = ( (k,v) for k,v in d.items() if k not in levels)
if aggregate:
for k,v in content: r.setdefault(k,list()).append(v)
else:
r.append(dict(content))
return result
output:输出:
byCity = dictNesting(data,"location","team",aggregate=True)
{ 'Oklahoma City':
{'Thunder':
{'player': ['Kevin Durant'], 'points': [15]}},
'Los Angeles':
{'Lakers':
{'player': ['Jeremy Lin', 'Kobe Bryant'], 'points': [22, 51]},
'Clippers':
{'player': ['Blake Griffin'], 'points': [26]}
}
}
lakersPlayers = byCity["Los Angeles"]["Lakers"]["player"]
# ['Jeremy Lin', 'Kobe Bryant']
lakersPoints = sum(byCity["Los Angeles"]["Lakers"]["points"])
# 73
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.