[英]If values are equal in dict return the keys python
I have a dict of keys and values. 我有一个键和值的字典。 I need to compare the values of all the keys if there are the same values in the dict and then return the keys, which have the same values.
如果字典中有相同的值,我需要比较所有键的值,然后返回具有相同值的键。
b = {(1, 1): '4', (2, 1): '4',(3,1):'8',(4,2):'9',(2,4):'10'}
p = dict(zip(b.values(),b.keys()))
The output I'm getting: 我得到的输出:
{'4': (2, 1), '8': (3, 1), '9': (4, 2), '10': (2, 4)}
Expected output: 预期产量:
{(1, 1): '4', (2, 1): '4'}
A single liner using dict comprehension
: 使用
dict comprehension
单个班轮:
>>> {k:v for k, v in b.items() if list(b.values()).count(v) > 1}
Here to determine if other values also contain the same, we check for the count to be greater than one. 为了确定其他值是否也包含相同值,我们检查计数是否大于1。
#driver values : #driver值:
IN : b = {(1, 1): '4', (2, 1): '4',(3,1):'8',(4,2):'9',(2,4):'10'}
OUT : {(1, 1): '4', (2, 1): '4'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.