我有一系列基因,我想将它们与属性列表和颜色相关联:
gene_A_color = red
gene_B_color = blue
gene_A_property = ['a','b','c']
gene_B_property = ['d','e','f']
出于绘图的目的,我想创建一个字典,我可以在其中使用属性值作为键来获取颜色或基因,如下所示:
#lookup dictionary
{'a': ['red', 'gene_A']
'b': ['red', 'gene_A']
'c': ['red', 'gene_A']
'd': ['blue' 'gene_B']
'e': ['blue' 'gene_B']
'f': ['blue' 'gene_B']}
lookup[a][0] = red
lookup[a][1] = gene_A
我一开始就是这样,但如果我丢失了基因名称,只能颠倒列表:
lookup_dict = defaultdict(list)
lookup_dict['red'] = ['a','b','c']
lookup_dict['blue'] = ['d','e','f']
inverted = defaultdict(list)
for k, v in lookup_dict.items():
inverted[v].append( k )
#inverted
{'a': 'red'
'b': 'red'
'c': 'red'
'd': 'blue'
'e': 'blue'
'f': 'blue' }
建议?