无法散列的类型列表错误,尽管使用字符串

时间:2019-06-06 12:23:17

标签: python python-3.x dictionary

我正在使用字典,有时会收到以下错误消息:

Traceback (most recent call last):
  File "./process_blasthit.py", line 637, in <module>
    prot_names_dict_path, options)
  File "./process_blasthit.py", line 491, in extract_dists
    prot_names_dict, options)
  File "./process_blasthit.py", line 326, in scan_file
    product, options)
  File "./process_blasthit.py", line 253, in add_to_env_dict
    if product not in env_dict[str(cessor_type)]:
TypeError: unhashable type: 'list'

如您所见,在字典中访问键之前,我已经尝试过将键(cessor_type)转换为字符串,但是它仍然无法正常工作。

此外,当我打印出type(cessor_type)告诉我它是一个字符串实例之前,

此外,前几个登录名(即env_dict [str(censor_type)])工作正常。

这是我尝试访问字典的功能:

def add_to_env_dict(env_queue, cessor_type, env_dict, succ_counter, 
                    product, options):

  if cessor_type=="succ":
    env_queue_new=env_queue[1:]
  elif cessor_type=="pred":
    env_queue_new=env_queue[max(0,len(env_queue)-options["env_k"]):]
  else:
      stderr.write("Cessor type needs to be either \"succ\" or \"pred\"")
      raise SystemExit

  # the key of env_dict is ["pred"/"succ"][product of blasthit]
  # the values are env_k-tuples (stored as lists) of predecessors/successors 
  # with additional information on each predecessor/successor
  print(type(cessor_type))
  if product not in env_dict[str(cessor_type)]:
    env_dict[cessor_type][product]=[env_queue_new]
  else:
    env_dict[cessor_type][product].append(env_queue_new)

  return env_dict

也许我在这里监督了一些明显的事情,但是我似乎无法弄清楚我在做什么错。我很感谢任何提示。

0 个答案:

没有答案