如何对 python 中数组的 JSON 个对象进行排序?
How to sort JSON objects of arrays in python?
我想根据键“分数”对数组的 JSON 对象进行排序。这是示例数据:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
},
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
}
]
}
]
示例输出:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
}
]
我尝试了这个问题中给出的解决方案,但是我的JSON数据被这个弄乱了。这是我尝试过的:
for i in range(len(inputData)):
sample_data_sort = dict(inputData)
sample_data_sort = sorted(inputData[i]['submissions'], key=lambda x : x['score'], reverse=True)
print(sample_data_sort)
我得到的输出:
[
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
],
[
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
如你所见,我把残缺不全的 JSON 找回来了。能否请您提供一些提示来纠正此问题,以便我得到的输出与输入相似?
Python字典条目以key:value对出现,可以通过键名进行引用。
如果允许修改原点JSON对象。
就这样:
def sort_submissions_of_items(json_list):
for item in json_list:
subs = item.get("submissions", [])
if isinstance(subs, list):
subs.sort(key=lambda x: x.get("score", 0), reverse=True)
sort_submissions_of_items(inputData)
试试这个
只需对内部字典进行排序,然后通过列表推导组合它们
并且会保证数据的原始结构
a = [
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
},
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
}
]
}
]
result = [
{
"name": i["name"],
"submissions": sorted(i["submissions"], key=lambda x: x["score"], reverse=True)
}
for i in a
]
print(result)
输出
[{'name': 'Hilary Carr', 'submissions': [{'name': 'Laudantium deleniti beatae fuga.', 'date': '05/12/2021', 'score': 37}]}, {'name': 'Frederick Williamson', 'submissions': [{'name': 'Animi facere excepturi.', 'date': '01/02/2021', 'score': 100}, {'name': 'Expedita architecto voluptas autem veniam.', 'date': '03/05/2009', 'score': 47}]}]
关键在这段代码
...sorted(inputData[i]['submissions']...
您没有对全部数据进行排序。相反,您正在做的是:
- 你取出原始数组中的每个元素(
inputData[i]
)
- 您只选择了
submissions
字段
- 您根据值
score
对 submissions
数组进行排序
你想要的解决方案大概是这样的
outputData = [{**i, 'submissions': sorted(i['submissions'], key= lambda j: j['score'], reverse=True)} for i in inputData]
我建议通读它并将其与您的原始解决方案进行比较 :D
我想根据键“分数”对数组的 JSON 对象进行排序。这是示例数据:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
},
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
}
]
}
]
示例输出:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
}
]
我尝试了这个问题
for i in range(len(inputData)):
sample_data_sort = dict(inputData)
sample_data_sort = sorted(inputData[i]['submissions'], key=lambda x : x['score'], reverse=True)
print(sample_data_sort)
我得到的输出:
[
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
],
[
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
如你所见,我把残缺不全的 JSON 找回来了。能否请您提供一些提示来纠正此问题,以便我得到的输出与输入相似?
Python字典条目以key:value对出现,可以通过键名进行引用。
如果允许修改原点JSON对象。
就这样:
def sort_submissions_of_items(json_list):
for item in json_list:
subs = item.get("submissions", [])
if isinstance(subs, list):
subs.sort(key=lambda x: x.get("score", 0), reverse=True)
sort_submissions_of_items(inputData)
试试这个
只需对内部字典进行排序,然后通过列表推导组合它们
并且会保证数据的原始结构
a = [
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
},
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
}
]
}
]
result = [
{
"name": i["name"],
"submissions": sorted(i["submissions"], key=lambda x: x["score"], reverse=True)
}
for i in a
]
print(result)
输出
[{'name': 'Hilary Carr', 'submissions': [{'name': 'Laudantium deleniti beatae fuga.', 'date': '05/12/2021', 'score': 37}]}, {'name': 'Frederick Williamson', 'submissions': [{'name': 'Animi facere excepturi.', 'date': '01/02/2021', 'score': 100}, {'name': 'Expedita architecto voluptas autem veniam.', 'date': '03/05/2009', 'score': 47}]}]
关键在这段代码
...sorted(inputData[i]['submissions']...
您没有对全部数据进行排序。相反,您正在做的是:
- 你取出原始数组中的每个元素(
inputData[i]
) - 您只选择了
submissions
字段 - 您根据值
score
对
submissions
数组进行排序
你想要的解决方案大概是这样的
outputData = [{**i, 'submissions': sorted(i['submissions'], key= lambda j: j['score'], reverse=True)} for i in inputData]
我建议通读它并将其与您的原始解决方案进行比较 :D