455,767 Members | 1,303 Online Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

# get keys with the same values

 P: n/a 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 Jun 27 '08 #1
13 Replies

 P: n/a Nader: 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) That's magic. 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? You can create a new dict where the keys are the values of the input dict and the values are a list of the keys of the original dict. So scanning the keys, values of the input dict, you can fill the second dict. Then you can scan the second dict, and create a list that contains only value lists longer than one. Bye, bearophile Jun 27 '08 #2

 P: n/a On Thu, 12 Jun 2008 03:58:53 -0700 (PDT), Nader wrote: >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)} That's not a dictionary, it's a syntax error. If you actually have a dictionary you could say d = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4} dd = {} for key, value in d.items(): try: dd[value].append(key) except KeyError: dd[value] = [key] Possibly dd is now what you really want; if you really want what you said you want you could use [l for l in dd.values() if len(l) 1] >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 David C. Ullrich Jun 27 '08 #3

 P: n/a On Jun 12, 1:35 pm, bearophileH...@lycos.com wrote: Nader: 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) That's magic. 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? You can create a new dict where the keys are the values of the input dict and the values are a list of the keys of the original dict. So scanning the keys, values of the input dict, you can fill the second dict. Then you can scan the second dict, and create a list that contains only value lists longer than one. Bye, bearophile Is it niet possible with one or two statement, maybe with list comprehension. For exmple: l = [(k,v) for k in d.keys() for v in d.values() | en here we need some extra logic (v = 1)] I don;t konw how we can define a logic statement in a list comprehension. It will be very compact, if it would possible. Nader Jun 27 '08 #4

 P: n/a On Jun 12, 1:41 pm, David C. Ullrich wrote: 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)} That's not a dictionary, it's a syntax error. If you actually have a dictionary you could say d = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4} dd = {} for key, value in d.items(): try: dd[value].append(key) except KeyError: dd[value] = [key] Possibly dd is now what you really want; if you really want what you said you want you could use [l for l in dd.values() if len(l) 1] 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 David C. Ullrich Thank for your type about the syntax error. This an example example, the keys of my dictionary are tuples: d = {(37.75, 42.22): 1 , (37.51, 40.02): 3 (45.55, 24.27): 4 (47.08, 30.99) : 1} But what I will is to get all keys which has the same valus. And not the keys that have value more than 1! Nader Jun 27 '08 #5

 P: n/a On Jun 12, 1:48*pm, Nader

 P: n/a On Jun 12, 2:05 pm, Chris

 P: n/a Nader >import itertools, functools, operatord = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4}get = functools.partial(operator.getitem, d)for (k,v) in itertools.groupby(sorted(d, key=get), key=get): print k, list(v) 1 ['a', 'e'] 2 ['c'] 3 ['b', 'd'] 4 ['f'] or if you want a dictionary: >>dict((k,list(v)) for (k,v) in itertools.groupby(sorted(d, key=get), key=get)) {1: ['a', 'e'], 2: ['c'], 3: ['b', 'd'], 4: ['f']} -- Duncan Booth http://kupuguy.blogspot.com Jun 27 '08 #8

 P: n/a On Jun 12, 2:15*pm, Nader

 P: n/a On Jun 12, 6:41*am, David C. Ullrich wrote: 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} dd = {} for key, value in d.items(): * try: * * dd[value].append(key) * except KeyError: * * dd[value] = [key] Instead of all that try/except noise, just use the new defaultdict: >>from collections import defaultdictd = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4}dd = defaultdict(list)for key, value in d.items(): ... dd[value].append(key) ... >>for k,v in dd.items(): ... print k,':',v ... 1 : ['a', 'e'] 2 : ['c'] 3 : ['b', 'd'] 4 : ['f'] -- Paul Jun 27 '08 #10

 P: n/a In article , Nader wrote: >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)} That's not a dictionary, it's a syntax error. If you actually have a dictionary you could say d = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4} dd = {} for key, value in d.items(): try: dd[value].append(key) except KeyError: dd[value] = [key] Possibly dd is now what you really want; if you really want what you said you want you could use [l for l in dd.values() if len(l) 1] >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 David C. Ullrich Thank for your type about the syntax error. This an example example, the keys of my dictionary are tuples: d = {(37.75, 42.22): 1 , (37.51, 40.02): 3 (45.55, 24.27): 4 (47.08, 30.99) : 1} But what I will is to get all keys which has the same valus. That's exactly what the code I posted does. And not the keys that have value more than 1! It doesn't do that. Or if you prefer, it doesn't do that! Instead of looking at it and trying to figure out what it does, which of course can be tricky, try _running_ the code. Then print dd. Then print [l for l in dd.values() if len(l) 1] . > Nader -- David C. Ullrich Jun 27 '08 #11

 P: n/a In article <3f**********************************@e39g2000hsf. googlegroups.com>, Paul McGuire wrote: >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} dd = {} for key, value in d.items(): * try: * * dd[value].append(key) * except KeyError: * * dd[value] = [key] Instead of all that try/except noise, just use the new defaultdict: That's certainly much better. The machine where I am in the early morning is still stuck with Python 1.5.3... >from collections import defaultdictd = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4}dd = defaultdict(list)for key, value in d.items(): ... dd[value].append(key) ... >for k,v in dd.items(): ... print k,':',v ... 1 : ['a', 'e'] 2 : ['c'] 3 : ['b', 'd'] 4 : ['f'] -- Paul -- David C. Ullrich Jun 27 '08 #12

 P: n/a Paul McGuire from collections import defaultdictd = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4}dd = defaultdict(list)for key, value in d.items(): ... dd[value].append(key) ... >for k,v in dd.items(): ... print k,':',v ... 1 : ['a', 'e'] 2 : ['c'] 3 : ['b', 'd'] 4 : ['f'] Or use the little understood dict.setdefault method which has been with us from time immemorial... >>d = {'a' : 1, 'b' : 3, 'c' : 2,'d' : 3,'e' : 1,'f' : 4}dd = {}for k, v in d.items(): .... dd.setdefault(v, []).append(k) .... >>dd {1: ['a', 'e'], 2: ['c'], 3: ['b', 'd'], 4: ['f']} >>> -- Nick Craig-Wood

 P: n/a You could use my mseqdict implementation of a sorted dict. http://home.arcor.de/wolfgang.grafen...s/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...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 Jun 27 '08 #14

### This discussion thread is closed

Replies have been disabled for this discussion. 