How to check if two keys in a dict have the same values

It seems such an obvious thing that it seems to me that I'm missing something, but how do you know if two different keys in the same dictionary have the same meaning? For example, if you have a test dictionary with the keys a , b and c , and the keys a and b have a value of 10, how would you understand this? (Regarding the question, please accept a large number of keys, say 100, and you do not know how many duplicates there are, if there are several sets of duplicates, or if there are duplicates at all). Thanks.

+6
source share
6 answers

To detect all of these cases:

 >>> import collections >>> d = {"a": 10, "b": 15, "c": 10} >>> value_to_key = collections.defaultdict(list) >>> for k, v in d.iteritems(): ... value_to_key[v].append(k) ... >>> value_to_key defaultdict(<type 'list'>, {10: ['a', 'c'], 15: ['b']}) 

@hivert makes a great point that this only works if the values โ€‹โ€‹are hashed. If this is not the case, then there is no good O (n) solution (unfortunately). This is the best I can think of:

 d = {"a": [10, 15], "b": [10, 20], "c": [10, 15]} values = [] for k, v in d.iteritems(): must_insert = True for val in values: if val[0] == v: val[1].append(k) must_insert = False break if must_insert: values.append([v, [k]]) print [v for v in values if len(v[1]) > 1] #prints [[[10, 15], ['a', 'c']]] 
+6
source
 len(dictionary.values()) == len(set(dictionary.values())) 

This suggests that the only thing you want to know is if there are any duplicate values, and not which values โ€‹โ€‹are duplicates, which I assumed from your question. Let me know if I misunderstood the question.

Basically this is just a check to see if any entries were deleted when the dictionary values โ€‹โ€‹were cast for an object that by definition has no duplicates.

If the above does not work for your purposes, this should be the best solution:

 set(k for k,v in d.items() if d.values().count(v) > 1)) 

Basically the second version just checks if there is more than one entry that will be deleted if you try to jump out of the list.

+6
source

You can specify which duplicate values, using the reverse index - where the key is the duplicate value, and the value is the set of keys that have this value (this will work as long as the values โ€‹โ€‹in the input dictionary are hashed):

 from collections import defaultdict d = {'w':20, 'x':10, 'y':20, 'z':30, 'a':10} dd = defaultdict(set) for k, v in d.items(): dd[v].add(k) dd = { k : v for k, v in dd.items() if len(v) > 1 } dd => {10: set(['a', 'x']), 20: set(['y', 'w'])} 

From this last result, it is easy to get a set of keys with duplicate values:

 set.union(*dd.values()) => set(['y', 'x', 'a', 'w']) 
+4
source
 dico = {'a':0, 'b':0, 'c':1} result = {} for val in dico: if dico[val] in result: result[dico[val]].append(val) else: result[dico[val]] = [val] >>> result {0: ['a', 'b'], 1: ['c']} 

Then you can filter the result key that has a value (list) with more than one element, for example. duplicate found

+2
source

Build another dict that matches the values โ€‹โ€‹of the first dict for all keys that hold this value:

 import collections inverse_dict = collections.defaultdict(list) for key in original_dict: inverse_dict[original_dict[key]].append(key) 
+1
source
 keys = set() for key1 in d: for key2 in d: if key1 == key2: continue if d[key1] == d[key2]: keys |= {key1, key2} 

i.e. what ฮ˜ (nยฒ) what you want. The reason is that dict does not provide ฮ˜ (1) a key search for a given value. Therefore, it is better to rethink your options for a data structure if this is not enough.

0
source

Source: https://habr.com/ru/post/950121/


All Articles