Write a Python function to remove items with identical values from Dictionary.
Input: {1:'a',2:'b',3:'a'}, Output: {2:'b'}
Input: {1:'a',2:'a',3:'a'}, Output:{}
dct = {1:'a',2:'b',3:'a'}
dct2 = {1:'a',2:'a',3:'a'}
print({k:v for k, v in dct.items() if list(dct.values()).count(v) == 1})
print({k:v for k, v in dct2.items() if list(dct2.values()).count(v) == 1})
from collections import Counter
Input=eval(input())
temp=Counter(Input.values())
key=[]
for i in temp.keys():
if temp[i]!=1:
for j in Input.keys():
if Input[j]==i:
key.append(j)
for i in key:
Input.pop(i)
print(Input)
使用set函数
second_dict = {
"name": "鸣人",
"age": 22,
"sex": "男",
"title": "六代火影",
"heigh": "179cm"
}
first_dict = {
"name": "鸣人",
"age": 22,
"sex": "女",
"title": "六代火影"
}
value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }
print(value)