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

标签 python dictionary

  • 名单如下
  • 偏好字典低于
  • 如果除 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/

    相关文章:

    python - 在 PostgreSQL、Python、Jinja2 上编码

    python - 如何绘制 Pandas 计数的分组条形图

    python - 如何使用 python-docx 将页码添加到每个页面的页脚?

    python - 在字典中附加列表的最佳方法是什么?

    dictionary - 字典 vs NamedTuples

    python - 最安全的加密算法

    python - 每当使用无服务器关闭 ec2 时发送电子邮件

    python - 从 python 中的字典获取键列表/键集的最有效方法是什么?

    python - 从字典中获取值

    python - 如何以 Pythonic 方式将此元组列表转换为此字典?