Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29479 > unrolled thread
| Started by | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| First post | 2012-09-19 07:17 -0400 |
| Last post | 2012-09-19 10:12 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
A little morning puzzle Neal Becker <ndbecker2@gmail.com> - 2012-09-19 07:17 -0400
Re: A little morning puzzle Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-19 14:34 +0300
Re: A little morning puzzle Paul Rubin <no.email@nospam.invalid> - 2012-09-19 10:12 -0700
| From | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| Date | 2012-09-19 07:17 -0400 |
| Subject | A little morning puzzle |
| Message-ID | <mailman.907.1348053436.27098.python-list@python.org> |
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?
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2012-09-19 14:34 +0300 |
| Message-ID | <qotfw6e4566.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #29479 |
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) }
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-09-19 10:12 -0700 |
| Message-ID | <7x627andgk.fsf@ruckus.brouhaha.com> |
| In reply to | #29479 |
Neal Becker <ndbecker2@gmail.com> 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))
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web