dict devono essere hashable, che le liste non sono becase sono mutabili. Puoi cambiare una lista dopo averla fatta. Pensa a quanto sarebbe difficile provare a mantenere una dict quando i dati usati come chiavi cambiano; non ha alcun senso. Immaginate questo scenario
>>> foo = [1, 2]
>>> bar = {foo: 3}
>>> foo.append(4)
e vedrete perché Python non tenta di supportare elenchi come chiavi.
La soluzione più ovvia è utilizzare tuple anziché elenchi come chiavi.
>>> d = {[1, 2, 3]: 1, [2, 3]: 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> d = {(1, 2, 3): 1, (2, 3): 3}
>>> d
{(2, 3): 3, (1, 2, 3): 1}
>>> d[2, 3]
3
fonte
2010-04-19 22:10:31
non è possibile, le chiavi dict devono essere immutabili. usa invece le tuple. – SilentGhost
Beh, come potrei farlo? D ((1,2,3)) = 1 restituisce anche un errore. – Chris
Sembra che tu stia usando '(...)' quando intendi usare '[...]'. 'D [1, 2, 3] = 1' dovrebbe funzionare bene. –