get keys with the same values

Wolfgang Grafen wolfgang.grafen at ericsson.com
Tue Jun 17 11:18:13 CEST 2008


You could use my mseqdict implementation of a sorted dict.

http://home.arcor.de/wolfgang.grafen/Python/Modules/Modules.html

swap:
This method can only be applied when all values of the dictionary are 
immutable. The Python dictionary cannot hold mutable keys! So swap 
doesn't work if only one of the values has the type list or dictionary. 
Tuples and instances of classes are save as long as they don't emulate 
lists or dictionaries.

from 
http://home.arcor.de/wolfgang.grafen/Python/Modules/seqdict/Mseqdict.html:

 >>> x=seqdict.mseqdict(dict)
 >>> x['Bild']='painting'
 >>> x['Ziel']='goal'
 >>> x['Tor'] ='goal'
 >>> x # A small German - English dictionary
mseqdict(
['gewinnen', 'deshalb', 'Abend', 'aber', 'Bild', 'Erkennung', 
'Fl\366te', 'Ziel', 'Tor'],
{'Tor': ['goal'], 'Ziel': ['goal'], 'gewinnen': ['gain'], 'deshalb': 
['therefore'], 'Abend': ['evening'], 'aber': ['but'], 'Bild': 
['picture', 'painting'], 'Erkennung': ['recognition'], 'Fl\366te': 
['flute']})

 >>> x.swap()
 >>> x # A small English - German dictionary
mseqdict(
['gain', 'therefore', 'evening', 'but', 'picture', 'painting', 
'recognition', 'flute', 'goal'],
{'but': ['aber'], 'recognition': ['Erkennung'], 'painting': ['Bild'], 
'flute': ['Fl\366te'], 'gain': ['gewinnen'], 'goal': ['Ziel', 'Tor'], 
'evening': ['Abend'], 'therefore': ['deshalb'], 'picture': ['Bild']})

Best regards

Wolfgang


Nader schrieb:
> Hello,
> 
> I have a dictionary and will get all keys which have the same values.
> 
> d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)}
> 
> I will something as :
> 
> d.keys(where their values are the same)
> 
> With this statement I can get two lists for this example:
> l1= ['a','e']
> l2=['b','d']
> 
> Would somebody tell me how I can do it?
> 
> Regards,
> Nader



More information about the Python-list mailing list