- 列表如下
- 偏好字典如下
- 如果除了
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)
type
键对字典进行排序,是这样吗? - Thunder CoderHR
,在上面的例子中没有出现,但它可能会出现在不同的输入中。 - sim