gpt4 book ai didi

python - 如何打印字典的键,在值列表中搜索值

转载 作者:行者123 更新时间:2023-12-01 11:11:58 25 4
gpt4 key购买 nike

我知道这是重复的。但是,该特定问题的答案对我不起作用。链接到问题 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])

关于python - 如何打印字典的键,在值列表中搜索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59188177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com