数据转化小案例

data = [
{
"url": "/user/",
"group": 1,
"action": "list"
},
{
"url": "/add/",
"group": 1,
"action": "add"
},

{
"url": "/role/",
"group": 2,
"action": "add"
}
]

dic = {}

for d in data:
gid = d["group"]
if not gid in dic:
dic[gid] = {
"urls": [d["url"],],
"actions": [d["action"],]
}
else:
dic[gid]["urls"].append(d["url"])
dic[gid]["actions"].append(d["action"])
print(dic)
'''
{1:
{'urls': ['/user/', '/add/'],
'actions': ['edit', 'add']},
2:
{'urls': ['/role/'],
'actions': ['add']}}
'''
posted @ 2020-12-03 17:43  Feng_fu  阅读(91)  评论(0编辑  收藏  举报