[英]Accessing list of dictionaries in python and sort it using value (Nested dictionary)
[英]Sort Nested List value using Dictionary value
我有一个嵌套列表字典。 当列表与标签匹配时,我需要按内部字典对列表(上层列表)进行排序,并按 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)
]
创建一个字典,您可以在其中将列表存储为值,将名称标签存储为键。 要填充字典,请遍历列表列表,然后遍历相应列表中的字典。 找到保存名称标签的字典,并将标签和列表添加到字典中。
然后您可以对字典的键/标签进行排序,然后遍历排序的键并按排序顺序检索您的列表。
如果这 - sorted(all_contact_data_response, key=lambda k: k["value"] and k["label"]==property_name)
- 有效,请尝试 [ sorted(all_contact_data_response, key=lambda k: k["value"] and k["label"]==property_name),]
但是,我不知道,你在变量 all_contact_data_response 中有什么
我的想法是从内部字典中弹出value
键,并将其作为临时变量添加到外部列表中。
# data contains the JSON as a dictionary.
for idx1, item1 in enumerate(data):
value = None
for idx2, item2 in enumerate(item1):
if item2["label"] == "Name":
value = item2["value"]
break
data[idx1].append(value)
现在,一旦我们在外部作用域中拥有该值,我们就可以轻松地使用它来对列表进行排序。
result = sorted(data, key=lambda i: i[-1])
更进一步,您可以删除额外的变量。
for item in result:
item.pop(-1)
好的。 如果我正确理解问题,请尝试此操作
def foo(list_item):
for a in list_item:
if a['label'] == 'Name':
return a['value']
res = sorted(all_contact_data_response, reverse=True, key = lambda list_item: foo(list_item))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.