Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A little morning puzzle

Reply
Thread Tools

A little morning puzzle

 
 
Neal Becker
Guest
Posts: n/a
 
      09-19-2012
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?

 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      09-19-2012
Neal Becker writes:

> 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?


Literally-ish:
{ key for key, val in ds[0].items() if all(val == d[key] for d in ds) }
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      09-19-2012
Neal Becker <> writes:
> 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?


Untested, and uses a few more comparisons than necessary:

# ds = [dict1, dict2 ... ]

d0 = ds[0]
ks = set(k for k in d0 if all(d[k]==d0[k] for d in ds))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A little morning puzzle Peter Otten Python 9 09-27-2012 05:58 PM
Re: A little morning puzzle Dwight Hutto Python 0 09-19-2012 12:22 PM
Re: A little morning puzzle Antoon Pardon Python 0 09-19-2012 12:13 PM
Re: A little morning puzzle Dwight Hutto Python 0 09-19-2012 12:01 PM
Re: A little morning puzzle Peter Otten Python 0 09-19-2012 11:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57