python - 矩阵作为字典键

标签 python dictionary numpy

我刚刚开始使用 numpy 及其 matrix 模块(非常非常有用!),我想使用矩阵对象作为字典的键,所以我检查了 matrix 是否实现了 __hash__ 方法:

>>> from numpy import matrix
>>> hasattr(matrix, '__hash__')
True

确实如此!很好,这意味着它可以成为字典的键:

>>> m1 = matrix('1 2 3; 4 5 6; 7 8 9')
>>> m1
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
>>> m2 = matrix('1 0 0; 0 1 0; 0 0 1')
>>> m2
matrix([[1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
>>> matrix_dict = {m1: 'first', m2: 'second'}

成功了!现在,让我们继续测试:

>>> matrix_dict[m1]
'first'
>>> matrix_dict[matrix('1 2 3; 4 5 6; 7 8 9')]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: matrix([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

什么?所以,它适用于同一个矩阵,但它不适用于另一个内容完全相同的矩阵?让我们看看 __hash__ 返回什么:

>>> hash(m1)
2777620
>>> same_as_m = matrix('1 2 3; 4 5 6; 7 8 9')
>>> hash(same_as_m)
-9223372036851998151
>>> hash(matrix('1 2 3; 4 5 6; 7 8 9')) # same as m too
2777665

因此,numpymatrix__hash__ 方法为相同的 matrix 返回不同的值。

这样对吗?那么,这是否意味着它不能用作字典键?如果不能使用,为什么要实现 __hash__

最佳答案

将可变对象用作字典的键是错误的,因为它的哈希值应该在您更改数据时立即更改,但插入时使用的值将保留。

在我的测试中,Python 3.2.2 的 numpy 引发了 TypeError:

TypeError: unhashable type: 'matrix'

但是在 Python 2.7 上它仍然允许散列,但是当你改变数据时散列值永远不会改变,所以它作为字典键是非常无用的,因为许多 matrix 对象被添加到具有相同散列的字典中将降低哈希表的性能,因此插入将是 O(n^2) 而不是 O(1)

也许他们没有删除哈希值以避免破坏 Python 2.x 上的某些 API,但不要依赖它!

关于python - 矩阵作为字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8813226/

相关文章:

python - QueryDict 和 MultiValueDict 有什么区别?

python - 为什么 pip 在 Debian 上安装 texttract 失败?

python - 如何使用列表/字典理解在 Python 中获取唯一值

python - dict.get 返回空而不是默认值

python - 如何在 Python 中从 Numpy 矩阵创建列表

python - 使用 Pandas 提取包含特定字符的数据

python - 如何将某些条目的列数不同的自定义数据集表示并加载到 sci-kit 学习中

java - ContainsValue 与 equals 方法 TreeMap

python - 如何找到两个日期之间的中位数月份?

python - 限制数组的值