Re: A little morning puzzle

Discussion in 'Python' started by Ian Kelly, Sep 19, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Wed, Sep 19, 2012 at 6:13 AM, Antoon Pardon
    <> wrote:
    > On 19-09-12 13:17, Neal Becker wrote:
    >> I have a list of dictionaries. They all have the same keys. I want to find the
    >> set of keys where all the dictionaries have the same values. Suggestions?

    > common_items = reduce(opereator.__and__, [set(dct.iteritems()) for dct
    > in lst])
    > common_keys = set([item[0] for item in common_items])


    You can use dictviews for that:

    common_items = reduce(operator.__and__, (d.viewitems() for d in ds))
    common_keys = [item[0] for item in common_items]
     
    Ian Kelly, Sep 19, 2012
    #1
    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. Neal Becker

    A little morning puzzle

    Neal Becker, Sep 19, 2012, in forum: Python
    Replies:
    2
    Views:
    186
    Paul Rubin
    Sep 19, 2012
  2. Peter Otten

    Re: A little morning puzzle

    Peter Otten, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    187
    Peter Otten
    Sep 19, 2012
  3. Dwight Hutto

    Re: A little morning puzzle

    Dwight Hutto, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    161
    Dwight Hutto
    Sep 19, 2012
  4. Peter Otten

    Re: A little morning puzzle

    Peter Otten, Sep 19, 2012, in forum: Python
    Replies:
    9
    Views:
    180
    Prasad, Ramit
    Sep 27, 2012
  5. Antoon Pardon

    Re: A little morning puzzle

    Antoon Pardon, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    177
    Antoon Pardon
    Sep 19, 2012
Loading...

Share This Page