我知道这是重复的。但是,该特定问题的答案对我不起作用。链接到问题here.
如何从字典的值中打印字典的键?
我的代码:
xdict = {
"Phenylalanine": ["UUU", "UUC"], "Leucine": ["UUA", "CUU", "CUC", "CUA", "CUG", "UUG"],
"Isoleucine": ["AUU", "AUC", "AUA"], "Methionine": "AUG", "Valine": ["GUU", "GUC", "GUA", "GUG"],
"Serine": ["UCU", "UCC", "UCA", "UCG"], "Proline": ["CCU", "CCC", "CCA", "CCG"],
"Threonine": ["ACU", "ACC", "ACA", "ACG"], "Alanine": ["GCU", "GCC", "GCA", "GCG"],
"Tyrosine": ["UAU", "UAC"], "Histidine": ["CAU", "CAC"], "Glutamine": ["CAA", "CAG"],
"Asparagine": ["AAU", "AAC"], "Lysine": ["AAA", "AAG"], "Asparatic Acid": ["GAU", "GAC"],
"Glutamic Acid": ["GAA", "GAG"], "Cysteine": ["UGU", "UGC"], "Trytophan": "UGG",
"Arginine": ["CGU", "CGC", "CGA", "CGG", "AGG", "AGA"], "Serine": ["AGU", "AGC"],
"Glycine": ["GGU", "GGC", "GGA", "GGG"]
}
a = input("Enter your DNA sequence: ")
print(list(xdict.keys())[list(xdict.values()).index(a)])
最佳答案
创建反向查找字典更容易:
>>> lookup_dict = {k: key for key, values in xdict.items() for k in values}
>>> lookup_dict["UUC"]
'Phenylalanine'
您可以将其应用于代码,如下所示:
lookup_dict = {k: key for key, values in xdict.items() for k in values}
a = input("Enter your DNA sequence: ")
print(lookup_dict[a])