如何创建一个字典的优先级列表

4
  • 列表如下
  • 偏好字典如下
  • 如果除了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)

请在您的问题中更新您尝试过的代码。 - quamrana
根据您的代码,我理解您正在尝试按type键对字典进行排序,是这样吗? - Thunder Coder
@ThunderCoder 是的,没错。 - sim
你的例子中没有包含对 HR 的引用。什么是 HR? - Derek
HR,在上面的例子中没有出现,但它可能会出现在不同的输入中。 - sim
4个回答

4
您只需执行 src.sort(key = lambda x : preference[x["type"]]) 即可对列表进行排序。

他也想要删除重复的类型。 - Newton8989

2
这个解决方案会按照id对所有元素进行分组,并根据优先级对这些组进行排序(以便首先显示Owner,最后显示HR),然后只需从每个组中选择第一个元素即可。
from collections import defaultdict

src = [
  {
    "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"
  }
]
preference = {'Owner': 1, 'Manager':2, 'Employ':3, 'HR': 4 }

d = defaultdict(list)
# group all the records by id
for item in src:
    d[item['id']].append(item)

# sort each group by the preference
for item in d.values():
    item.sort(key=lambda x: preference[x['type']])

# select only the first from each group
result = [item[0] for item in d.values()]

print(result)

输出:

[{'id': '11', 'name': 'son', 'email': 'n@network.com', 'type': 'Owner'}, {'id': '21', 'name': 'abc', 'email': 'abc@network.com', 'type': 'Manager'}]

1
你可以创建一个优先队列:

from queue import PriorityQueue

priority = {'Owner': 1, 'Manager':2, 'employ':3, 'HR': 4 }

q = PriorityQueue()
for elem in list_:
    p = priority[elem['type']]
    q.put((p, id(elem), elem))

你也可以根据 type 对列表进行排序:

priority_list = sorted(list_, key=lambda x: priority[x['type']], reverse=True)

你能解释一下吗?我不太理解PriorityQueue。 - sim
请提供一个英文文本,我才能为您翻译成中文。 - Mark

0

好的,这是我的尝试!

虽然不太美观,但似乎能正常工作。

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"
  }
]

new = dict( Owner = 0, Manager = 0, Employ = 0, HR = 0 )

for a in list_ :
    type_ = a[ 'type' ]
    if type_ == 'Owner':
        new[ 'Owner' ] += 1
    if type_ == 'Manager':
        new[ 'Manager' ] += 1
    if type_ in [ 'Employ', 'Manager' ]:
        new[ 'Employ' ] += 1
    new[ 'HR' ] += 1

print( new )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接