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


Groups > comp.lang.python > #3881

Re: suggestions, comments on an "is_subdict" test

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'python': 0.07; 'builtin': 0.09; 'from:addr:python': 0.09; 'value:': 0.09; '>>>': 0.12; 'solutions,': 0.12; 'def': 0.13; 'wrote:': 0.14; '"""test': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'possibly': 0.16; 'have:': 0.19; 'maybe': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'received:84': 0.25; 'testing': 0.28; 'missed': 0.29; 'this.': 0.30; 'key,': 0.31; 'all,': 0.31; 'to:addr:python-list': 0.32; 'reference': 0.34; 'header:User-Agent:1': 0.35; 'reply- to:addr:python.org': 0.35; 'some': 0.37; 'ways': 0.38; 'comments': 0.39; 'to:addr:python.org': 0.39; 'i.e.': 0.40; 'would': 0.40; 'simple': 0.60; 'reply-to:no real name:2**0': 0.72; 'header:Reply- To:1': 0.72; 'idiomatic': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AhUIAE6bsU3Unw4S/2dsb2JhbACYEY1Md4hwux6FdgSSNIJV
Date Fri, 22 Apr 2011 16:18:49 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9
MIME-Version 1.0
To python-list@python.org
Subject Re: suggestions, comments on an "is_subdict" test
References <mailman.747.1303480525.9059.python-list@python.org> <4db19756$0$81473$e4fe514c@news.xs4all.nl>
In-Reply-To <4db19756$0$81473$e4fe514c@news.xs4all.nl>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.754.1303485531.9059.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 82.94.164.166
X-Trace 1303485531 news.xs4all.nl 81478 [::ffff:82.94.164.166]:58314
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3881

Show key headers only | View raw


On 22/04/2011 15:57, Irmen de Jong wrote:
> On 22-4-2011 15:55, 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 would use:
>
> test_dct.items()<= base_dct.items()
>
In Python 2:

 >>> test_dct = {"foo": 0, "bar": 1}
 >>> base_dct = {"foo": 0, "bar": 1, "baz": 2}
 >>>
 >>> test_dct.items() <= base_dct.items()
False

In Python 3:

 >>> test_dct = {"foo": 0, "bar": 1}
 >>> base_dct = {"foo": 0, "bar": 1, "baz": 2}
 >>> test_dct.items() <= base_dct.items()
True

YMMV

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


Thread

suggestions, comments on an "is_subdict" test Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-04-22 15:55 +0200
  Re: suggestions, comments on an "is_subdict" test Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-04-22 16:57 +0200
    Re: suggestions, comments on an "is_subdict" test MRAB <python@mrabarnett.plus.com> - 2011-04-22 16:18 +0100
      Re: suggestions, comments on an "is_subdict" test Raymond Hettinger <python@rcn.com> - 2011-04-23 00:23 -0700
    Re: suggestions, comments on an "is_subdict" test Paul Rubin <no.email@nospam.invalid> - 2011-04-23 00:26 -0700

csiph-web