Re: suggestions, comments on an "is_subdict" test

2011-04-23 Thread Paul Rubin
Irmen de Jong 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()) -- http://mail.python.org/mailman/listinfo/python-list

Re: suggestions, comments on an "is_subdict" test

2011-04-23 Thread Raymond Hettinger
On Apr 22, 8:18 am, MRAB 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 > >

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Steven D'Aprano
On Fri, 22 Apr 2011 07:38:38 -0700, Chris Rebert wrote: > Also, the following occurs to me as another idiomatic, perhaps more > /conceptually/ elegant possibility, but it's /practically/ speaking > quite inefficient (unless perhaps some dict view tricks can be > exploited): > > def is_subdict(sub

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread MRAB
On 22/04/2011 21:31, Vlastimil Brom wrote: Thanks everyone for your opinions and suggestions! I especially like the all(...) approaches of MRAB and Peter Otten, however, the set conversion like set(test_dct.items())<= set(base_dct.items()) True looks elegant too. That works only if the values

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Vlastimil Brom
Thanks everyone for your opinions and suggestions! I especially like the all(...) approaches of MRAB and Peter Otten, however, the set conversion like >>> set(test_dct.items()) <= set(base_dct.items()) True looks elegant too. In both approaches I can get rid of the negated comparison and the additi

Re: dict comparison [was: suggestions, comments on an "is_subdict" test]

2011-04-22 Thread Peter Otten
Zero Piraeus wrote: > : > > On 22 April 2011 13:30, Peter Otten <__pete...@web.de> wrote: > def is_subdict(test_dct, base_dct): >> ... return test_dct <= base_dct and all(test_dct[k] == base_dct[k] >> for ... k in test_dct) >> ... > is_subdict({1:0}, {2:0}) >> Traceback (most recent c

dict comparison [was: suggestions, comments on an "is_subdict" test]

2011-04-22 Thread Zero Piraeus
: On 22 April 2011 13:30, Peter Otten <__pete...@web.de> wrote: def is_subdict(test_dct, base_dct): > ...     return test_dct <= base_dct and all(test_dct[k] == base_dct[k] for > ... k in test_dct) > ... is_subdict({1:0}, {2:0}) > Traceback (most recent call last): >  File "", line 1, in

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Peter Otten
Zero Piraeus wrote: >>> Anything wrong with this? >>> >>> def is_subdict(test_dct, base_dct): >>> return test_dct <= base_dct and all(test_dct[k] == base_dct[k] for >>> k in test_dct) >> >> It may raise a KeyError. > > Really? That was what ``test_dct <= base_dct and`` ... is supposed to > preven

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Zero Piraeus
: >> Anything wrong with this? >> >> def is_subdict(test_dct, base_dct): >>     return test_dct <= base_dct and all(test_dct[k] == base_dct[k] for >> k in test_dct) > > It may raise a KeyError. Really? That was what ``test_dct <= base_dct and`` ... is supposed to prevent. Have I missed something?

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread MRAB
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(tes

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Peter Otten
Zero Piraeus wrote: > : > >>> 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. > > Anything wrong with this? > > def is_subdict(test_dct, base_dct): > return tes

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Irmen de Jong
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

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Wolfgang Rohdewald
On Freitag 22 April 2011, Vlastimil Brom wrote: > check whether all items of a given dictionary are > present in a reference dictionary I would not call this is_subdict. That name does not clearly express that all keys need to have the same value. set(subdict.items()) <= set(reference.items()) s

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Zero Piraeus
: >> 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. Anything wrong with this? def is_subdict(test_dct, base_dct): return test_dct <= base_dct and all(test_dct[k]

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Chris Rebert
On Fri, Apr 22, 2011 at 6:55 AM, 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):

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread Peter Otten
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 it

Re: suggestions, comments on an "is_subdict" test

2011-04-22 Thread MRAB
On 22/04/2011 14: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

suggestions, comments on an "is_subdict" test

2011-04-22 Thread Vlastimil Brom
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