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/

    相关文章:

    java - 如何将 "Pair"或 n 大小插入列表集合而不是组成 HashMap?

    python - 将 defaultdict(list) 保存到 csv 文件

    python - python2.7中递归函数的执行顺序

    Python 3 - 在用户将鼠标悬停在 GUI 窗口上之前,主线程未检测到后台线程中的键盘中断

    python - 链表中的冒泡排序。如何使其按艺术家姓名排序?

    python - 如何从特定字典键的值列表中删除单词?

    swift - 字典不符合 ExtensibleCollectionType

    python - 如何加快 PostgreSQL 中的更新/替换操作?

    python - GAE 数据存储查询 ConjunctionNode 错误

    arrays - 如何在 iOS Swift 中将值数组附加到 tableView 中?