我正在尝试根据需要格式化数据结构。我要从字典列表中创建字典列表:
[
{
"nameWithOwner": "Repository-Org/documentation",
"languages": {
"edges": [
{
"node": {
"name": "HCL"
},
"size": 3366
},
{
"node": {
"name": "Shell"
},
"size": 1435
},
{
"node": {
"name": "Ruby"
},
"size": 868
},
{
"node": {
"name": "HTML"
},
"size": 30650
},
{
"node": {
"name": "CSS"
},
"size": 59692
}
]
}
},
{
"nameWithOwner": "Repository-Org/integration-api",
"languages": {
"edges": [
{
"node": {
"name": "JavaScript"
},
"size": 245648
},
{
"node": {
"name": "Python"
},
"size": 7483
},
{
"node": {
"name": "Shell"
},
"size": 20050
},
{
"node": {
"name": "Dockerfile"
},
"size": 1343
},
{
"node": {
"name": "Roff"
},
"size": 492
}
]
}
}
{
"nameWithOwner": "Repository-Org/demo",
"languages": {
"edges": [
{
"node": {
"name": "Jupyter Notebook"
},
"size": 5125
},
{
"node": {
"name": "Python"
},
"size": 8623
}
]
}
},
{
"nameWithOwner": "Repository-Org/github-link",
"languages": {
"edges": []
}
},
{
"nameWithOwner": "Repository-Org/intelligence-config",
"languages": {
"edges": []
}
}
]
我想对其进行格式化并格式化以获取像这样的字典列表,但我没有找到访问嵌套数据或了解是否可以做到的方法
[
{
"name": "Repository-Org/documentation",
"language": {
"HCL": 3366,
"Shell": 1435,
"Ruby": 868,
"HTML": 30650,
"CSS": 59692
}
},
{
"name": "Repository-Org/integration-api",
"language": {
"JavaScript": 245648,
"Python": 7483,
"Shell": 20050,
"Dockerfile": 1343,
"Roff": 492
}
},
{
"name": "Repository-Org/demo",
"language": {
"Jupyter Notebook": 5125,
"Python": 8623
}
},
{
"name": "Repository-Org/github-link",
"language": {}
},
{
"name": "Repository-Org/intelligence-config",
"language": {}
},
]
此刻我做到了:
dicta = {}
array = []
for repository in variables:
for key,value in repository.items() :
dicta[key]=value
array.append(dicta)
print(array)
得到了这个结果:
[
{'nameWithOwner': 'Repository-Org/documentation', 'languages': {'edges': [
{'node': {'name': 'HCL'
}, 'size': 3366
},
{'node': {'name': 'Shell'
}, 'size': 1435
},
{'node': {'name': 'Ruby'
}, 'size': 868
},
{'node': {'name': 'HTML'
}, 'size': 30650
},
{'node': {'name': 'CSS'
}, 'size': 59692
}
]
}
}
]
[
{'nameWithOwner': 'Repository-Org/integration-api', 'languages': {'edges': [
{'node': {'name': 'JavaScript'
}, 'size': 245648
},
{'node': {'name': 'Python'
}, 'size': 7483
},
{'node': {'name': 'Shell'
}, 'size': 20050
},
{'node': {'name': 'Dockerfile'
}, 'size': 1343
},
{'node': {'name': 'Roff'
}, 'size': 492
}
]
}
},
{'nameWithOwner': 'Repository-Org/integration-api', 'languages': {'edges': [
{'node': {'name': 'JavaScript'
}, 'size': 245648
},
{'node': {'name': 'Python'
}, 'size': 7483
},
{'node': {'name': 'Shell'
}, 'size': 20050
},
{'node': {'name': 'Dockerfile'
}, 'size': 1343
},
{'node': {'name': 'Roff'
}, 'size': 492
}
]
}
}
]
[
{'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
{'node': {'name': 'Jupyter Notebook'
}, 'size': 5125
},
{'node': {'name': 'Python'
}, 'size': 8623
}
]
}
},
{'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
{'node': {'name': 'Jupyter Notebook'
}, 'size': 5125
},
{'node': {'name': 'Python'
}, 'size': 8623
}
]
}
},
{'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
{'node': {'name': 'Jupyter Notebook'
}, 'size': 5125
},
{'node': {'name': 'Python'
}, 'size': 8623
}
]
}
}
]
[
{'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
}
}
]
[
{'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
}
},
{'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
}
}
]
我应该改变以获得我想要的东西吗?
您可以在这里找到一个有效的示例:https://repl.it/repls/FastAlphanumericKnowledge
答案 0 :(得分:1)
您可以尝试:
original = [
{
"nameWithOwner": "Repository-Org/documentation",
"languages": {
"edges": [
{
"node": {
"name": "HCL"
},
"size": 3366
},
{
"node": {
"name": "Shell"
},
"size": 1435
},
{
"node": {
"name": "Ruby"
},
"size": 868
},
{
"node": {
"name": "HTML"
},
"size": 30650
},
{
"node": {
"name": "CSS"
},
"size": 59692
}
]
}
},
{
"nameWithOwner": "Repository-Org/integration-api",
"languages": {
"edges": [
{
"node": {
"name": "JavaScript"
},
"size": 245648
},
{
"node": {
"name": "Python"
},
"size": 7483
},
{
"node": {
"name": "Shell"
},
"size": 20050
},
{
"node": {
"name": "Dockerfile"
},
"size": 1343
},
{
"node": {
"name": "Roff"
},
"size": 492
}
]
}
},
{
"nameWithOwner": "Repository-Org/demo",
"languages": {
"edges": [
{
"node": {
"name": "Jupyter Notebook"
},
"size": 5125
},
{
"node": {
"name": "Python"
},
"size": 8623
}
]
}
},
{
"nameWithOwner": "Repository-Org/github-link",
"languages": {
"edges": []
}
},
{
"nameWithOwner": "Repository-Org/intelligence-config",
"languages": {
"edges": []
}
}
]
new_dicts = []
for d in original:
temp_dict = {}
temp_dict['name'] = d['nameWithOwner']
temp_dict['languages'] = {}
for l in d['languages']['edges']:
temp_dict['languages'][l['node']['name']] = l['size']
new_dicts.append(temp_dict)
print(new_dicts)
[{'name': 'Repository-Org/documentation', 'languages': {'HCL': 3366, 'Shell': 1435, 'Ruby': 868, 'HTML': 30650, 'CSS': 59692}}, {'name': 'Repository-Org/integration-api', 'languages': {'JavaScript': 245648, 'Python': 7483, 'Shell': 20050, 'Dockerfile': 1343, 'Roff': 492}}, {'name': 'Repository-Org/demo', 'languages': {'Jupyter Notebook': 5125, 'Python': 8623}}, {'name': 'Repository-Org/github-link', 'languages': {}}, {'name': 'Repository-Org/intelligence-config', 'languages': {}}]
答案 1 :(得分:1)
尝试这样
reps = {}
for x in origdata:
if not x["nameWithOwner"] in reps:
reps[x["nameWithOwner"]]={}
for y in x["languages"]["edges"]:
reps[x["nameWithOwner"]][y["node"]["name"]] = y["size"]
res = []
for x in reps:
res.append({"name":x, "language":reps[x]})
# pprint for nice output
from pprint import pprint
pprint(res)