get keys with the same values

Chris cwitts at gmail.com
Thu Jun 12 09:22:25 EDT 2008


On Jun 12, 2:15 pm, Nader <n.em... at gmail.com> wrote:
> On Jun 12, 2:05 pm, Chris <cwi... at gmail.com> wrote:
>
>
>
> > On Jun 12, 1:48 pm, Nader <n.em... at gmail.com> wrote:
>
> > > On Jun 12, 1:35 pm, bearophileH... at 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
>
> > If you are going to use this reverse look-up alot you'd be better off
> > building another dictionary with the original values being keys and
> > the original keys being values, if it is used infrequently enough you
> > can search for it with result_list = [k for k,v in dictionary.items()
> > if v == search_value]
>
> Thank you! It is the anwser which I was looking for. [(k,v) for k,v
> in  d.items() if v is pattern].
> But I don't understand what tou mean of "reverse look-up a lot"! I
> have to read some informations inclusive (latitudes and longitudes)
> form a file and after some processing to save part of this information
> to other file.
> Why do I make a new dictionary?
>
> Nader

If you are just going to perform the lookup once or twice then it's
fine to traverse (step through) your original dictionary.  If you are
going to look up data often though it might be a better idea to build
another dictionary with the reverse of your original dictionary as it
will yield faster results.

For example, your original dictionary of values is built and then you
perform a handful of operations and move on, then use the list
comprehension to get your data and move on.  If, on the other hand,
you build your dictionary and then maybe iterate over a file and need
to look-up the information for every line in the file it would be
better suited to build a new dictionary that transposed the key and
value pairs for less resource intensive and faster operation. eg:

reverse_dict = {}
for k,v in original_dict:
    if v in reverse_dict:
        reverse_dict[v].append(k)
    else:
        reverse_dict[v] = [k]

Then once that is built and you want to find which keys in the
original dictionary have the value of "1" you can just do
"list_of_keys = reverse_dict[1]".

Essentially if you are going to do alot of searching for values that
match values found in a dictionary you would be better off to create
the new data structure.

Hope that helps.



More information about the Python-list mailing list