TypeError: unhashable type: 'dict' in python

假装没事ソ 提交于 2019-12-11 07:24:19

问题


I'm getting an error message about dictionaries despite having never used a dictionary anywhere in my code

Here is my code:

mm=[]
soln=[]
for i in range(len(momvec)):
    string = str(momvec[i])
    num = string[2:]
    mm.append(Symbol('x'+num))
    print num
    print mm
    soln.append(solve(mom[i]-momvec[i],mm))
print type(MFK[0])
for m in range(0,len(MFK)):
    for i in range(0,len(mm)):
        print MFK[m]
        MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()

What I am trying to do is replace some items in the list MFK with what is indicated by mm with the contents of soln iteratively.

The traceback:

Traceback (most recent call last):
  File "MEA.py", line 313, in <module>
    MFK_final(numMoments)
  File "MEA.py", line 242, in MFK_final
    MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()
  File "/cluster/soft/linux64/epd/lib/python2.7/site-packages/sympy/core/function.py", line 1270, in __new__
    pts = sorted(set(point), key=default_sort_key)
TypeError: unhashable type: 'dict'

values for mom, momvec and MFK respectively:

[x2 - y_0**2]

[ym2]

[-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0), 4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)]

Printing output to screen it appears as though the first loop goes fine, then it throws the error in the title. Does anyone know how I could solve this?

Many thanks


回答1:


Your soln[i] is a dictionary, but it needs to be a SymPy expression (the one you plan to substitute into MFK[m] for mm[i]).




回答2:


From your description, I can't reproduce your error. That is, I made a Python file:

from sympy import *

x2 = Symbol("x2")
y_0 = Symbol("y_0")
ym2 = Symbol("ym2")
c_0 = Symbol("c_0")
c_1 = Symbol("c_1")
c_2 = Symbol("c_2")
yx1 = Symbol("yx1")

mom = [x2 - y_0**2]
momvec = [ym2]
MFK = [-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0), 4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)]

mm=[]
soln=[]
for i in range(len(momvec)):
    string = str(momvec[i])
    num = string[2:]
    mm.append(Symbol('x'+num))
    print num
    print mm
    soln.append(solve(mom[i]-momvec[i],mm))
print type(MFK[0])
for m in range(0,len(MFK)):
    for i in range(0,len(mm)):
        print MFK[m]
        MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit()

and it simply outputs

2
[x2]
<class 'sympy.core.add.Add'>
-2*c_0*y_0*(y_0 - 1) - 2*c_0*(-y_0**2 + yx1*(y_0**2/yx1 + 1)) + 2*c_1*(0.5*c_2 - 0.5*y_0)
4.0*c_0*y_0**2 - 4.0*c_0*y_0 + 2.0*c_1*c_2 - 2.0*c_1*y_0 - (-y_0**2 + yx1*(y_0**2/yx1 + 1))*(8.0*c_0*y_0 - 8.0*c_0 + 2.0*c_1)

without any errors or tracebacks.

However, if you do find the point in the code where the hidden dictionary appears, the reason dictionaries shouldn't be hashable (and therefore usable as keys in other dictionaries) is because they're mutable. If you use one as a key and then later change a value somewhere deep in its structure, you could get weird x != x style errors. You could fix such an error with a hashable frozen dictionary...



来源:https://stackoverflow.com/questions/16859642/typeerror-unhashable-type-dict-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!