Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
==如何适用于python中的词典_Python_Dictionary - Fatal编程技术网

==如何适用于python中的词典

==如何适用于python中的词典,python,dictionary,Python,Dictionary,下面的代码是用来比较两个字典的,如果它们相等,则什么也不做。如果它们不相等并且存在重复的关键点,则抛出错误。如果dict2中的键不重复,则将键和值添加到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

下面的代码是用来比较两个字典的,如果它们相等,则什么也不做。如果它们不相等并且存在重复的关键点,则抛出错误。如果dict2中的键不重复,则将键和值添加到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
我遇到的问题是,使用==的字典比较显示为false,即使字典中存储的对象在对象内部具有相同的值。唯一的区别是字典中对象的指针值。==方法是否不递归检查字典中存储的对象的相等性?另外,有人能解释一下python字典中的等式检查行为吗

跳过回溯的代码的读数为:

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

映射上的相等比较是递归执行的。但是,键和值本身也必须支持相等比较,以便不进行简单的比较。这是通过方法完成的,该方法
coeffs\u-angle。coeffs\u-angle
似乎没有实现。

具有相同值的对象不相等,除非您定义它们的
\uuuueq\uuu
方法。如果dict1.\uuu包含\uuuuuuu(键):,请不要执行
。这是不必要的复杂。只需在
中使用
如果键入dict1:
。谢谢,我刚刚更改了dict1.\uuu包含\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?我从来没有写过eq方法。这种解决方法只适用于这种特定情况。实现
\uuuuuuu eq\uuuuu()
将使该类在任何情况下都能正常工作。有关如何正确编写
\uuuuuuuuueq\uuuuu
的说明,请参阅。特别是,如果您可能会与其他类型的对象进行比较,请参阅关于返回
NotImplemented
。非常感谢。我最终解决了这个问题。我还发现了一篇关于堆栈溢出的文章,它解释了_eq__;()方法递归工作的方式。
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