“Series objects are mutable and cannot be hashed” error

后端 未结 2 1417
后悔当初
后悔当初 2020-11-30 02:36

I am trying to get the following script to work. The input file consists of 3 columns: gene association type, gene name, and disease name.

cols = [\'Gene typ         


        
2条回答
  •  忘掉有多难
    2020-11-30 03:22

    Shortly: gene_name[x] is a mutable object so it cannot be hashed. To use an object as a key in a dictionary, python needs to use its hash value, and that's why you get an error.

    Further explanation:

    Mutable objects are objects which value can be changed. For example, list is a mutable object, since you can append to it. int is an immutable object, because you can't change it. When you do:

    a = 5;
    a = 3;
    

    You don't change the value of a, you create a new object and make a point to its value.

    Mutable objects cannot be hashed. See this answer.

    To solve your problem, you should use immutable objects as keys in your dictionary. For example: tuple, string, int.

提交回复
热议问题