Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3869 > unrolled thread
| Started by | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| First post | 2011-04-22 15:55 +0200 |
| Last post | 2011-04-23 00:26 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| Date | 2011-04-22 15:55 +0200 |
| Subject | suggestions, comments on an "is_subdict" test |
| Message-ID | <mailman.747.1303480525.9059.python-list@python.org> |
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.)
Thanks in advance for the suggestions,
regards,
vbr
[toc] | [next] | [standalone]
| From | Irmen de Jong <irmen.NOSPAM@xs4all.nl> |
|---|---|
| Date | 2011-04-22 16:57 +0200 |
| Message-ID | <4db19756$0$81473$e4fe514c@news.xs4all.nl> |
| In reply to | #3869 |
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() -irmen
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-04-22 16:18 +0100 |
| Message-ID | <mailman.754.1303485531.9059.python-list@python.org> |
| In reply to | #3879 |
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
[toc] | [prev] | [next] | [standalone]
| From | Raymond Hettinger <python@rcn.com> |
|---|---|
| Date | 2011-04-23 00:23 -0700 |
| Message-ID | <f2d50a1d-13c4-47f1-813c-a636fe3314f2@j35g2000prb.googlegroups.com> |
| In reply to | #3881 |
On Apr 22, 8:18 am, MRAB <pyt...@mrabarnett.plus.com> wrote:
> 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
That is because it is spelled differently in Python 2.7:
>>> test_dct = {"foo": 0, "bar": 1}
>>> base_dct = {"foo": 0, "bar": 1, "baz": 2}
>>> test_dct.viewitems() <= base_dct.viewitems()
True
Raymond
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2011-04-23 00:26 -0700 |
| Message-ID | <7xd3kdig22.fsf@ruckus.brouhaha.com> |
| In reply to | #3879 |
Irmen de Jong <irmen.NOSPAM@xs4all.nl> writes: > I would use: > test_dct.items() <= base_dct.items() I think you need an explicit cast: set(test_dct.items()) <= set(base_dct.items())
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web