Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29479 > unrolled thread

A little morning puzzle

Started byNeal Becker <ndbecker2@gmail.com>
First post2012-09-19 07:17 -0400
Last post2012-09-19 10:12 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#29479 — A little morning puzzle

FromNeal Becker <ndbecker2@gmail.com>
Date2012-09-19 07:17 -0400
SubjectA 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]


#29481

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2012-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]


#29508

FromPaul Rubin <no.email@nospam.invalid>
Date2012-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