python - 使用字典值对嵌套列表值进行排序
问题描述
我有一个嵌套列表字典。我需要在匹配标签时按内部字典对列表(上级列表)进行排序,并在 python 中按值排序。例如,我想对列表进行排序(“label”=="Name" 并按内部字典的键“value”的值),这是我的字典,
[
[
{
"propertyId": 1,
"fieldName": "Name",
"value": "mahir contact 1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "name",
"label": "Name",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "string",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.248107"
},
{
"propertyId": 2,
"fieldName": "Email",
"value": "mahir_contact_1@gmail.com",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "email",
"label": "Email",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "email",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.253318"
},
{
"propertyId": 6,
"fieldName": "Contact creation date",
"value": "2020-11-24 05:57:01.216359",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactCreationDate",
"label": "Contact creation date",
"description": "default properties by roboket",
"fieldType": "Date picker",
"fieldDataType": "datetime",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": false,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.256631"
},
{
"propertyId": 11,
"fieldName": "Contact owner",
"value": "1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactOwner",
"label": "Contact owner",
"description": "default properties by roboket",
"fieldType": "Dropdown select",
"fieldDataType": "select",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": true,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.261312"
}
],
[
{
"propertyId": 3,
"fieldName": "Phone number",
"value": "+8801517179498",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "phoneNumber",
"label": "Phone number",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "number",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.115570"
},
{
"propertyId": 6,
"fieldName": "Contact creation date",
"value": "2020-11-24 05:58:04.070069",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactCreationDate",
"label": "Contact creation date",
"description": "default properties by roboket",
"fieldType": "Date picker",
"fieldDataType": "datetime",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": false,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.122804"
},
{
"propertyId": 11,
"fieldName": "Contact owner",
"value": "1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactOwner",
"label": "Contact owner",
"description": "default properties by roboket",
"fieldType": "Dropdown select",
"fieldDataType": "select",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": true,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.130811"
},
{
"propertyId": 1,
"fieldName": "Name",
"value": "mahir contact 2",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "name",
"label": "Name",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "string",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.111468"
}
]
]
我的预期输出:
[
[
{
"propertyId": 3,
"fieldName": "Phone number",
"value": "+8801517179498",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "phoneNumber",
"label": "Phone number",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "number",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.115570"
},
{
"propertyId": 6,
"fieldName": "Contact creation date",
"value": "2020-11-24 05:58:04.070069",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactCreationDate",
"label": "Contact creation date",
"description": "default properties by roboket",
"fieldType": "Date picker",
"fieldDataType": "datetime",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": false,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.122804"
},
{
"propertyId": 11,
"fieldName": "Contact owner",
"value": "1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactOwner",
"label": "Contact owner",
"description": "default properties by roboket",
"fieldType": "Dropdown select",
"fieldDataType": "select",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": true,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.130811"
},
{
"propertyId": 1,
"fieldName": "Name",
"value": "mahir contact 2",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "name",
"label": "Name",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "string",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 2,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:58:04.111468"
}
],
[
{
"propertyId": 1,
"fieldName": "Name",
"value": "mahir contact 1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "name",
"label": "Name",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "string",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.248107"
},
{
"propertyId": 2,
"fieldName": "Email",
"value": "mahir_contact_1@gmail.com",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "email",
"label": "Email",
"description": "default properties by roboket",
"fieldType": "Single line text",
"fieldDataType": "email",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.253318"
},
{
"propertyId": 6,
"fieldName": "Contact creation date",
"value": "2020-11-24 05:57:01.216359",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactCreationDate",
"label": "Contact creation date",
"description": "default properties by roboket",
"fieldType": "Date picker",
"fieldDataType": "datetime",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": false,
"hasOptions": false,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.256631"
},
{
"propertyId": 11,
"fieldName": "Contact owner",
"value": "1",
"objectTypeName": "Contact properties",
"groupName": "Contact information",
"name": "contactOwner",
"label": "Contact owner",
"description": "default properties by roboket",
"fieldType": "Dropdown select",
"fieldDataType": "select",
"propertyOwnerId": null,
"propertyOwnerAccountId": 1,
"default": true,
"hasOptions": true,
"id": 1,
"contactCreatorId": 1,
"contactOwnerId": 1,
"createdAt": "2020-11-24T05:57:01.261312"
}
]
]
提前感谢您的帮助。
解决方案
对于您的问题,我建议采用以下两步解决方案:
import json
with open("test.json") as json_file:
data = json.load(json_file)
labeled_contacts = {}
for contact_props in data:
for contact_prop in contact_props:
if contact_prop["label"] == "Name":
labeled_contacts[contact_prop["value"]] = contact_props
sorted_contacts = [
labeled_contacts[label] for label in sorted(labeled_contacts.keys(), reverse=True)
]
创建一个字典,您可以在其中将列表存储为值,将名称标签存储为键。要填充字典,请遍历列表列表,然后遍历相应列表中的字典。找到包含名称标签的字典,并将标签和列表添加到字典中。
然后您可以对字典的键/标签进行排序,然后遍历排序的键并按排序顺序检索您的列表。
推荐阅读
- google-sheets - 每行一次 COUNTIF,ARRAYFORMULA 中的花括号中有多个条件(Google 表格)
- inno-setup - Inno Setup 关闭已经打开的应用程序,无需用户交互
- css - 删除主题覆盖中 Material UI 组件的 CSS 规则
- data-structures - 如果恶意黑客试图在 Hash 键上强制冲突,你如何避免 Hash 中的冲突?
- javascript - 为什么下拉菜单在多个表中不起作用jQuery
- node.js - 无法正确模拟承诺和回调
- reactjs - React - 使用 forwardRef 转发道具
- laravel - Laravel hasMany和belongsToMany的重要性是什么
- audio - 如何从 Stm32 USB 音频设备获取数据并发送到 DAC?
- python - 将 1d numpy 数组的列表连接到 2d numpy