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


Groups > comp.lang.python > #3873

Re: suggestions, comments on an "is_subdict" test

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:" ': 0.03; 'subject:test': 0.05; 'dictionary': 0.07; '>>>>': 0.09; 'builtin': 0.09; 'exceptions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'value:': 0.09; '>>>': 0.12; 'solutions,': 0.12; 'def': 0.13; 'wrote:': 0.14; '"""test': 0.16; 'passed.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'possibly': 0.16; 'have:': 0.19; 'maybe': 0.21; 'code': 0.22; 'unsure': 0.23; 'object': 0.27; 'testing': 0.28; 'missed': 0.29; '(the': 0.30; 'seem': 0.30; 'this.': 0.30; 'comparison': 0.31; 'from:addr:web.de': 0.31; 'key,': 0.31; 'all,': 0.31; 'to:addr:python-list': 0.32; 'reference': 0.34; 'header:X-Complaints-To:1': 0.34; 'some': 0.37; 'case': 0.37; 'ways': 0.38; 'received:org': 0.38; 'anything': 0.38; 'ok,': 0.39; 'comments': 0.39; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'i.e.': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'simple': 0.60; 'unique': 0.63; 'worth': 0.64; 'special': 0.66; 'idiomatic': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: suggestions, comments on an "is_subdict" test
Date Fri, 22 Apr 2011 16:38:18 +0200
Organization None
References <BANLkTinazu41uVqxmkfTns0ZOHScZ1a3nQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084c898.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.749.1303483093.9059.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 82.94.164.166
X-Trace 1303483093 news.xs4all.nl 81479 [::ffff:82.94.164.166]:34811
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3873

Show key headers only | View raw


Vlastimil Brom wrote:

> Hi all,
> I'd like to ask for comments or advice on a simple code for testing a
> "subdict", i.e. check whether all items of a given dictionary are
> present in a reference dictionary.
> Sofar I have:
> 
> def is_subdict(test_dct, base_dct):
>     """Test whether all the items of test_dct are present in base_dct."""
>     unique_obj = object()
>     for key, value in test_dct.items():
>         if not base_dct.get(key, unique_obj) == value:
>             return False
>     return True
> 
> I'd like to ask for possibly more idiomatic solutions, or more obvious
> ways to do this. Did I maybe missed some builtin possibility?
> I am unsure whether the check  against an unique object() or the
> negated comparison are usual.?
> (The builtin exceptions are ok, in case anything not dict-like is
> passed. A cornercase like >>> is_subdict({}, 4)
>>>> True
> doesen't seem to be worth a special check just now.)

I would avoid the unique object because it's neither hard nor costly:

def is_subdict(test, base):
    return all(k in base and base[k] == v for k, v in test.iteritems())

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: suggestions, comments on an "is_subdict" test Peter Otten <__peter__@web.de> - 2011-04-22 16:38 +0200

csiph-web