get keys with the same values

Discussion in 'Python' started by Nader, Jun 12, 2008.

  1. Nader

    Nader Guest

    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
     
    Nader, Jun 12, 2008
    #1
    1. Advertising

  2. Nader

    Guest

    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 12, 2008
    #2
    1. Advertising

  3. Nader

    Nader Guest

    On Jun 12, 1:35 pm, 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
     
    Nader, Jun 12, 2008
    #3
  4. Nader

    Nader Guest

    On Jun 12, 1:41 pm, David C. Ullrich <> wrote:
    > 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


    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
     
    Nader, Jun 12, 2008
    #4
  5. Nader

    Chris Guest

    On Jun 12, 1:48 pm, Nader <> wrote:
    > On Jun 12, 1:35 pm, 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]
     
    Chris, Jun 12, 2008
    #5
  6. Nader

    Nader Guest

    On Jun 12, 2:05 pm, Chris <> wrote:
    > On Jun 12, 1:48 pm, Nader <> wrote:
    >
    >
    >
    > > On Jun 12, 1:35 pm, 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
     
    Nader, Jun 12, 2008
    #6
  7. Nader

    Chris Guest

    On Jun 12, 2:15 pm, Nader <> wrote:
    > On Jun 12, 2:05 pm, Chris <> wrote:
    >
    >
    >
    > > On Jun 12, 1:48 pm, Nader <> wrote:

    >
    > > > On Jun 12, 1:35 pm, 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.
     
    Chris, Jun 12, 2008
    #7
  8. Nader

    Paul McGuire Guest

    On Jun 12, 6:41 am, David C. Ullrich <> wrote:
    > 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.

    >

    <snip>
    >
    > 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]
    >

    <snip>

    Instead of all that try/except noise, just use the new defaultdict:

    >>> from collections import defaultdict
    >>>
    >>> d = {'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
     
    Paul McGuire, Jun 12, 2008
    #8
  9. 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
     
    Wolfgang Grafen, Jun 17, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    593
    Mike Wahler
    Oct 29, 2004
  2. Menghan Zheng
    Replies:
    1
    Views:
    278
    alex23
    Apr 20, 2010
  3. Cameron Simpson
    Replies:
    6
    Views:
    358
    alex23
    Apr 21, 2010
  4. Ami
    Replies:
    4
    Views:
    242
  5. Henrik Faber
    Replies:
    8
    Views:
    223
    Ethan Furman
    Jul 26, 2012
Loading...

Share This Page