I have code below that is supposed to compare two dictionaries and if they are equal do nothing. If they are not equal and there are duplicate keys, throw an error. If the keys in dict2 are not duplicates, add the key and value to dict1.
print 'dictionary 1 is', dict1
print 'dictionary 2 is', dict2
if dict1 == dict2:
pass #does nothing since the two dictionaries are the same
else:
for key, val in dict2.items():
if dict1.__contains__(key):
print 'the value at', key, 'is', val.write() #prints the information stored in val which is dict2[key]
print 'the other value at', key, 'is', dict1[key].write() #prints the information stored in dict1[key]
raise KeyError('the added keys must be unique. Key {0} is a duplicate.'\.format(key))
else:
dict1[key] = val
The issue I am having is the dictionary comparison using == is showing as false, even though the objects stored in the dictionary have the same values inside the objects. The only difference is the pointer values for the objects in the dictionary. Does the == method not recursively check the equality of objects stored in the dictionary? Additionally, can someone give an explanation of the equality checking behavior in dictionaries for python?
The readout for the code skipping the traceback is:
dictionary 1 is {1: <coeffs_angle.Coeffs_angle object at 0x118c7f710>, 2: <coeffs_angle.Coeffs_angle object at 0x118c7f790>, 3: <coeffs_angle.Coeffs_angle object at 0x118c7f810>, 4: <coeffs_angle.Coeffs_angle object at 0x118c7f890>, 5: <coeffs_angle.Coeffs_angle object at 0x118c7f910>, 6: <coeffs_angle.Coeffs_angle object at 0x118c7f990>}
dictionary 2 is {1: <coeffs_angle.Coeffs_angle object at 0x118e17dd0>, 2: <coeffs_angle.Coeffs_angle object at 0x118e17e50>, 3: <coeffs_angle.Coeffs_angle object at 0x118e17ed0>, 4: <coeffs_angle.Coeffs_angle object at 0x118e17f50>, 5: <coeffs_angle.Coeffs_angle object at 0x118e17fd0>, 6: <coeffs_angle.Coeffs_angle object at 0x118e24090>}
the value at 1 is 89.5 113.3
the other value at 1 is 89.5 113.3
the code throws an error after the readout above. To confirm that the information stored in the objects in the dictionary is identical, I will list the rest of the readout assuming no error was thrown.
the value at 2 is 87.9 109.47
the other value at 2 is 87.9 109.47
the value at 3 is 74.5 111.4
the other value at 3 is 74.5 111.4
the value at 4 is 63.3 125.6
the other value at 4 is 63.3 125.6
the value at 5 is 126.5 123
the other value at 5 is 126.5 123
the value at 6 is 84.8 116.4
the other value at 6 is 84.8 116.4