Howto find dict members from a list of keys

Discussion in 'Python' started by Alexander Eisenhuth, Mar 8, 2007.

  1. Hello,

    what algo do you use, when you want to find the dict values from d, with members
    of l. Following example:

    >>> d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
    >>> l = [7,8]
    >>> found_dic_members = <yourCode>
    >>> print found_dict_members

    [8,9]

    Thanks
    Alexander
     
    Alexander Eisenhuth, Mar 8, 2007
    #1
    1. Advertising

  2. En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
    <> escribió:

    > what algo do you use, when you want to find the dict values from d, with
    > members
    > of l. Following example:
    >
    > >>> d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
    > >>> l = [7,8]
    > >>> found_dic_members = <yourCode>
    > >>> print found_dict_members

    > [8,9]


    found_dic_members = [d[key] for key in l]


    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 8, 2007
    #2
    1. Advertising

  3. On Thu, 08 Mar 2007 09:20:20 +0100, Alexander Eisenhuth wrote:

    > Hello,
    >
    > what algo do you use, when you want to find the dict values from d, with members
    > of l. Following example:
    >
    > >>> d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
    > >>> l = [7,8]
    > >>> found_dic_members = <yourCode>
    > >>> print found_dict_members

    > [8,9]


    It depends.

    If all of the values in d are usable as keys (that is, all the values
    are hashable, so no lists or sets etc.) then you can build a
    reverse-lookup dictionary and use that for fast look-ups.

    >>> d = {1: 'a', 2: 'b', 3: 'c'}
    >>> rd = dict(zip(d.values(), d.keys()))
    >>> rd

    {'a': 1, 'c': 3, 'b': 2}

    But if you can't do that, you're stuck with a slow search through the
    entire dict:

    def reverse_search(d, target):
    """Return the first key of dict d that matches the target value."""
    for key, value in d.iteritems():
    if value == target:
    return key
    raise KeyError('no key found matching that value')


    In both cases though, you have to think about what you want to happen for
    duplicated values.



    --
    Steven D'Aprano
     
    Steven D'Aprano, Mar 8, 2007
    #3
  4. On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:

    > En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
    > <> escribió:
    >
    >> what algo do you use, when you want to find the dict values from d, with
    >> members
    >> of l. Following example:
    >>
    >> >>> d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
    >> >>> l = [7,8]
    >> >>> found_dic_members = <yourCode>
    >> >>> print found_dict_members

    >> [8,9]

    >
    > found_dic_members = [d[key] for key in l]


    *self stares at the line of code*

    *self thinks about what he just posted*

    *self realises with acute embarrassment that he's jumped to conclusions
    and completely misunderstood the Original Poster's question*

    Oops! I'll just be slinking away now...



    --
    Steven D'Aprano
     
    Steven D'Aprano, Mar 8, 2007
    #4
  5. En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
    <> escribió:

    > On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
    >
    >> found_dic_members = [d[key] for key in l]

    >
    > *self stares at the line of code*
    >
    > *self thinks about what he just posted*
    >
    > *self realises with acute embarrassment that he's jumped to conclusions
    > and completely misunderstood the Original Poster's question*
    >
    > Oops! I'll just be slinking away now...


    LOL! :)

    (...now! But when I saw your previous post, I had to check whether it was
    *me* who misunderstood the OP answering with a silly one-liner...)


    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 8, 2007
    #5
  6. Alexander Eisenhuth

    James Stroud Guest

    Gabriel Genellina wrote:
    > En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
    > <> escribió:
    >
    >> On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
    >>
    >>> found_dic_members = [d[key] for key in l]

    >>
    >> *self stares at the line of code*
    >>
    >> *self thinks about what he just posted*
    >>
    >> *self realises with acute embarrassment that he's jumped to conclusions
    >> and completely misunderstood the Original Poster's question*
    >>
    >> Oops! I'll just be slinking away now...

    >
    > LOL! :)
    >
    > (...now! But when I saw your previous post, I had to check whether it
    > was *me* who misunderstood the OP answering with a silly one-liner...)
    >
    >
    > --Gabriel Genellina
    >

    I think it was a silly one-liner.

    If you look closely at the example, the only mapping of

    [7, 8] ==> [8, 9]

    is

    [key, key] ==> [value, value]

    Look again at the example and consider the positional relationships.

    The answer, then, is trivial if not silly:

    [d[k] for k in l]

    It doesn't appear he as asking for the interesting case:

    [value, value] ==> [key, key]

    Its easy to see how Gabriel was thrown for a loop on this one.

    James
     
    James Stroud, Mar 8, 2007
    #6
  7. Yes it was the silly on-liner ... it was a bit ago I used it last time ... thanks

    Gabriel Genellina schrieb:
    > En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
    > <> escribió:
    >
    >> On Thu, 08 Mar 2007 05:26:22 -0300, Gabriel Genellina wrote:
    >>
    >>> found_dic_members = [d[key] for key in l]

    >>
    >> *self stares at the line of code*
    >>
    >> *self thinks about what he just posted*
    >>
    >> *self realises with acute embarrassment that he's jumped to conclusions
    >> and completely misunderstood the Original Poster's question*
    >>
    >> Oops! I'll just be slinking away now...

    >
    > LOL! :)
    >
    > (...now! But when I saw your previous post, I had to check whether it
    > was *me* who misunderstood the OP answering with a silly one-liner...)
    >
    >
    > --Gabriel Genellina
    >
     
    Alexander Eisenhuth, Mar 8, 2007
    #7
    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. Menghan Zheng
    Replies:
    1
    Views:
    278
    alex23
    Apr 20, 2010
  2. Cameron Simpson
    Replies:
    6
    Views:
    358
    alex23
    Apr 21, 2010
  3. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    229
    Steven D'Aprano
    Jul 25, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    103
    Neil Cerutti
    Jul 24, 2013
  5. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    111
    Oscar Benjamin
    Jul 24, 2013
Loading...

Share This Page