dic = {('UUU','UUC'):'F',('GUU','GUC','GUA','GUG'):'V'}
L = ['UUU', 'GUG', 'GUU']
如您所见,列表(L)的每个元素都在字典中作为键。现在我想用相应的值替换L的每个元素。输出将是:
output = ['F','V']
我该怎么做?
答案 0 :(得分:1)
一种方法是将密钥分解为单个元素,并从那些创建一个新的字典:
new_dic = {}
for k, v in dic.items():
for sub_k in k:
new_dic[sub_k] = v
现在只需循环查看列表:
output = [new_dic[i] for i in L]
您可以使用set
:
output = list(set(output))
答案 1 :(得分:1)
使用列表压缩:
In [1]: dic = {('UUU','UUC'):'F',('GUU','GUC','GUA','GUG'):'V'}
In [2]: L = ['UUU', 'GUG', 'GUU']
In [3]: list(set([v for k,v in dic.items() for x in L if x in k]))
Out [3]: ['V', 'F']