gpt4 book ai didi

python - 如何创建字典的优先级列表

转载 作者:行者123 更新时间:2023-12-04 12:04:49 25 4
gpt4 key购买 nike

  • 名单如下
  • 偏好字典低于
  • 如果除 type 之外的所有键和值那么将相同..
  • 需要比较type在偏好字典中最高顺序的每个列表中
  • 输出是最高阶字典的列表
  • list_ = [
    {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Owner"
    },
    {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Manager"
    },
    {
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Employ"
    },
    {
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Manager"
    }
    ]
    偏好词典 = {'Owner': 1, 'Manager':2, 'employ':3, 'HR': 4 }我预期的输出字典如下
    [{'id': '11', 'name': 'son', 'email': 'n@network.com', 'type': 'Owner'},
    {'id':'21','name': 'abc','email': 'abc@network.com','type': 'Manager'}]

    new_list = []
    for each in list_:
    if each['type'] in priority.keys():
    if each['id'] not in new_list:
    new_list.append(each)

    最佳答案

    你可以简单地做 src.sort(key = lambda x : preference[x["type"]])并且您的列表将被排序。

    关于python - 如何创建字典的优先级列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66970580/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com