首页 > 解决方案 > 使用字典值对嵌套列表值进行排序

问题描述

我有一个嵌套列表字典。我需要在匹配标签时按内部字典对列表(上级列表)进行排序,并在 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"
      }
    ]       
]

  

提前感谢您的帮助。

标签: pythonpython-3.x

解决方案


对于您的问题,我建议采用以下两步解决方案:

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)
]
  1. 创建一个字典,您可以在其中将列表存储为值,将名称标签存储为键。要填充字典,请遍历列表列表,然后遍历相应列表中的字典。找到包含名称标签的字典,并将标签和列表添加到字典中。

  2. 然后您可以对字典的键/标签进行排序,然后遍历排序的键并按排序顺序检索您的列表。


推荐阅读