我知道您不能将列表用作 Python 字典中的键,因为它是可变的,因此不可散列(或类似的东西)。但我似乎在 上得到了这个错误value
,而不是key
. 这是我的代码:
sp_eq = {}
sp_list = []
def build_sp_eq(row):
# dbrefs is a dict, i.e { reference_type : id_of_that_gene}
dbrefs = row.get('dbReference')
print('dbrefs - ' +str(dbrefs))
gene_ids = []
for k, v in dbrefs.items():
if k == 'GeneId':
gene_ids.append(v)
# new ID if more than 1 entrez reference
if len(gene_ids) > 1:
print('More than 1 GeneId for: ' +str(row.get('name')))
sp_eq[row.get('name')] = uuid.uuid4()
if len(gene_ids) == 0:
# new if 0 entrez references
sp_eq[row.get('name')] = uuid.uuid4()
sp_list.append(row.get('name'))
# if only one entrez ref, use the entrez uuid
elif len(gene_ids) == 1:
pdb.set_trace()
print('type of row.get('name'): ' +str(type(row.get('name'))))
sp_eq[row.get('name')] = entrez_eq.get(v)
这是解释器的输出:
dbrefs - {'HGNC': ['HGNC:4931']}
dbrefs - {'HGNC': ['HGNC:4931']}
dbrefs - {'HGNC': ['HGNC:4932']}
dbrefs - {'MGI': ['MGI:1920949'], 'GeneId': ['73699']}
type of row.get('name'): <class 'str'>
Traceback (most recent call last):
File "./gp_baseline.py", line 303, in <module>
make_namespace(x, parser)
File "./gp_baseline.py", line 247, in make_namespace
build_sp_eq(row)
File "./gp_baseline.py", line 144, in build_sp_eq
sp_eq[row.get('name')] = entrez_eq.get(v)
TypeError: unhashable type: 'list'
如您所见,key
在这种情况下,row.get('name')
, 是 String 类型,应该没问题。另外,据我了解,使用列表作为此处的值也应该没问题。关于这里可能发生的任何想法?