Ask Your Question

Revision history [back]

Dictionaries can only be built with hashable objects. You get the same trouble with

sage: {[1,2]: 0, [0]: 5}
Traceback (most recent call last):
...
TypeError: unhashable type: 'list'

Though, using lists as values is fine

sage: {1: [0,3], 'hello': [1,2]}
{1: [0, 3], 'hello': [1, 2]}

In order to use matrices or vectors as keys of a dictionary you need to set them immutable first.

sage: m1 = matrix(ZZ, 2, [1, 2, 3, 4])
sage: m2 = matrix(ZZ, 2, [1, 0, 1, 1])
sage: {m1: 0, m2: 1}
Traceback (most recent call last):
...
TypeError: unhashable type: 'list'
sage: {m1: 0, m2: 1}
{[1 0]
 [1 1]: 1,
 [1 2]
 [3 4]: 0}

Dictionaries can only be built with hashable objects. You get the same trouble with

sage: {[1,2]: 0, [0]: 5}
Traceback (most recent call last):
...
TypeError: unhashable type: 'list'

Though, using lists as values is fine

sage: {1: [0,3], 'hello': [1,2]}
{1: [0, 3], 'hello': [1, 2]}

In order to use matrices or vectors as keys of a dictionary you need to set them immutable first.

sage: m1 = matrix(ZZ, 2, [1, 2, 3, 4])
sage: m2 = matrix(ZZ, 2, [1, 0, 1, 1])
sage: {m1: 0, m2: 1}
Traceback (most recent call last):
...
TypeError: unhashable type: 'list'
sage: m1.set_immutable()
sage: m2.set_immutable()
sage: {m1: 0, m2: 1}
{[1 0]
 [1 1]: 1,
 [1 2]
 [3 4]: 0}

Note that the method set_mutable does not return anything but modifies the matrix itself.