TypeError:不可分类型:python中的'dict' [英] TypeError: unhashable type: 'dict' in python
问题描述
这是我的代码:
我收到一个关于字典的错误信息,尽管我的代码中没有使用字典
mm = []
soln = []
for i in range(len(momvec)):
string = str (momvec [i])
num = string [2:]
mm.append(Symbol('x'+ num))
print num
print mm
(0,len(MFK))
打印类型(MFK [0])
(m, $ b for i in range(0,len(mm)):
print MFK [m]
MFK [m] = Subs(MFK [m],mm [i],soln [i]) .doit()
我想要做的是替换列表中的某些项目 MFK
与 mm
一起指示,内容为 soln
/ p>
追溯:
追溯(最近的最后一次呼叫):
文件MEA.py,第313行,位于< module>
MFK_final(numMoments)
文件MEA.py,第242行,MFK_final
MFK [m] = Subs(MFK [m],mm [i],soln [i]) .doit()
文件/cluster/soft/linux64/epd/lib/python2.7/site-packages/sympy/core/function.py,第1270行,__new__
pts =已排序(set(point),key = default_sort_key)
TypeError:不可分类型:'dict'
分别为mom,momvec和MFK的值:
[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)]
打印输出到屏幕,看起来好像第一个循环很好,然后它会在标题中抛出错误。有没有人知道如何解决这个问题?
非常感谢
您的 soln [i]
是一个字典,但它需要是一个SymPy表达式(您计划替换为 MFK [m] / code> for
mm [i]
)。
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
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]
).
这篇关于TypeError:不可分类型:python中的'dict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!