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

Z

Zero Piraeus

:

...     return test_dct <= base_dct and all(test_dct[k] == base_dct[k] for
... k in test_dct)
...Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 3, in is_subdict
 File "<stdin>", line 3, in <genexpr>
KeyError: 1

I think you have to convert to sets before performing the <= comparisonto
get a proper subset test.

Huh. I thought I remembered that dict comparison worked like set
comparison (and my admittedly minimal testing seemed to confirm that).
Turns out it's actually "consistent, but not otherwise defined" beyond
equality.

http://docs.python.org/reference/expressions.html#id15

I maintain that the behaviour I expected makes more sense ;-) I wonder
whether making it work the way I want it to (dammit) would have been
as prohibitively expensive as the lexicographical comparison mentioned
in the footnote referenced in the above link?

-[]z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,146
Latest member
Vinay KumarNevatia_
Top