I am trying to compare two dictionaries for equality. The dictionaries have a nested dictionary structure. Reading the documentation, I understand that Python should be able to compare them; the only difference is that one has strings and the other has unicode strings (I'm on Python 2).
> state in states
False
> dict(state) == dict(states[0])
False
> json.dumps(state, sort_keys=True) == json.dumps(states[0], sort_keys=True)
True
The only difference is that the keys for the top-level and nested dictionaries are strings in one and unicode in the other but testing a toy example looks like Python doesn't care.
> state.keys() == states[0].keys()
True
> state.keys()
['slots', 'responses']
> states[0].keys()
[u'slots', u'responses']
> {'a':1, 'b': {'c':2}} == {u'a':1, u'b': {u'c':2}}
True
I already wrapped these dictionaries in a class and I can perform operations using JSON but I'd like to understand what's happening.
class OverloadedDict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
def __hash__(self):
return hash(json.dumps(dict(self), sort_keys=True))
def __eq__(self, other):
if isinstance(other, self.__class__):
return json.dumps(dict(self), sort_keys=True) == json.dumps(dict(other), sort_keys=True)
else:
return False
def __ne__(self, other):
return not (self == other)
I'd like to avoid having to convert them to JSON. Is there an easy and efficient way to compare them for equality and membership?
Update
@juanpa.arrivillaga in the comments suggested switching to unicode literals by virtue of using from __future__ import unicode_literals
. That did not work as the dictionaries are initialized with keyword arguments.
After importing unicode_literals
, dict({'a':1})
will be {u'a': 1}
but dict(a=1)
will still be {'a': 1}
.