TypeError:不可用的类型python可以解决吗?

时间:2015-03-10 14:34:44

标签: python python-2.7

我想更新DiseaseScenario.conn [newKey]这是一个集合,但我一直得到错误不可缓存。有办法解决这个问题吗?

DiseaseScenario.conn={}

    for items in dictList:
        for key,value in items.iteritems():
            flag=1
            for newKey,newValue in DiseaseScenario.conn.iteritems():
                if key==newKey:
                    //***************************///
                  //geting the error Unhashable type                     
                    tempValue=[value,newValue]
                    DiseaseScenario.conn[newKey].remove(value)
                    DiseaseScenario.conn[newKey].add(tempValue)
                    //*******************************************//     

                    flag=0
            if flag==1:
                DiseaseScenario.conn[key]=value

    print DiseaseScenario.conn

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您正试图将list放入set。你不能这样做,因为set项需要有一个固定的哈希值,这是可变的(可变的)内置python类型所没有的。

最简单的解决方案是将list更改为tuple(元组有点像无法就地更改的列表)。所以改变:

 tempValue=[value,newValue]

为:

 tempValue=(value,newValue)

当然,假设valuenewValue不是列表或其他可变类型。