gpt4 book ai didi

python - 用另一个列表遍历字典列表,并找到关联的键值

转载 作者:行者123 更新时间:2023-12-05 05:47:33 26 4
gpt4 key购买 nike

我正在编写代码,通过与以下 A、T、C 或 G 之一交换该位置,分别修改所有 3 个位置的 3 个字母序列。

我已经能够创建 3 个列表,其中初始元素的第一个、第二个或第三个位置被修改为其他 3 个不同字母之一。

我写了一本字典,对每个键(在本例中为氨基酸)及其对应的密码子序列(这将是我正在修改的元素)进行编码。 .

现在,我的目标是根据该字典检查每个修改后的列表元素,并查看它们对应于哪个字典键。我希望查看初始元素中的更改是否会更改与其关联的结果键。

我无法弄清楚如何继续;我怎样才能为修改后的列表的值获取相应的键?

到目前为止,这是我的代码:

thisdict = {
"Arg": ['CGT', 'CGC','CGA','CGG'],
"Phe": ['TTT','TTC'],
"Leu": ['TTA','TTG','CTT','CTC','CTA','CTG'],
"Ile": ['ATT','ATC'],
"Met": ['ATA','ATG'],
"Val": ['GTT','GTC','GTA','GTG'],
"Ser": ['TCT','TCC','TCA','TCG','AGT','AGC'],
"Pro": ['CCT,','CCC','CCA','CCG'],
"Ala": ['GCT','GCC','GCA','GCG'],
"Thr": ['ACT','ACC','ACA','ACG'],
"Tyr": ['TAT','TAC'],
"His": ['CAT','CAC'],
"Gln": ['CAA','CAG'],
"Asn": ['AAT','AAC'],
"Lys": ['AAA','AAG'],
'Asp': ['GAT','GAC'],
"Glu": ['GAA','GAG'],
"Cys": ['TGT','TGC'],
"Trp": ['TGA','TGG'],
"Gly": ['GGT','GGC','GGA','GGG'],
"end(*)":['TAA','TAG','AGA','AGG'],
}

def degenerated_sites(codon):
print(thisdict)

a_codon = list(codon)
b_codon = list(codon)
c_codon = list(codon)
nucleotides = ['A','C','T','G']
codons1=[]
codons2=[]
codons3=[]

for i in range(len(nucleotides)):
a_codon[0] = nucleotides[i]
first_site = "".join(a_codon)
codons1.append(first_site)
for i in range (len(nucleotides)):
b_codon[1] = nucleotides[i]
second_site = "".join(b_codon)
codons2.append(second_site)
for i in range (len(nucleotides)):
c_codon[2] = nucleotides[i]
third_site = "".join(c_codon)
codons3.append(third_site)
codons1.remove(codon)
codons2.remove(codon)
codons3.remove(codon)

print(codons1)
print(codons2)
print(codons3)

for key, value in thisdict.items():
for i in range(len(codons1)):
if value == codons1[i]:
print(key)
#I have encoded the 64 codons with their asssocaited amino acids in a dictionary. I have changed the codon site 1, 2, and 3 and stored them in lists. Now I am trying to check if the initial amino acid has been chagned, and determine the degeneracy of each site of the codon (0-fold if any change in causes amino acid change, 2-fold if only 2 out of 4 nucleotides cause amino acid change, and 4-fold if any change in nucleotide results in no change of amino acid)

degenerated_sites('CGT')
#I am confused why I am not getting any keys

我想要的结果是密码子上方的数字表示该位点的简并性,并将此代码扩展为更长的序列。

004
CGT

最佳答案

您需要检查密码子列表中的项目是否 dict 的值中,dict 是一个列表。所以 if codons1[i] in value

thisdict = {
"Arg": ['CGT', 'CGC','CGA','CGG'],
"Phe": ['TTT','TTC'],
"Leu": ['TTA','TTG','CTT','CTC','CTA','CTG'],
"Ile": ['ATT','ATC'],
"Met": ['ATA','ATG'],
"Val": ['GTT','GTC','GTA','GTG'],
"Ser": ['TCT','TCC','TCA','TCG','AGT','AGC'],
"Pro": ['CCT,','CCC','CCA','CCG'],
"Ala": ['GCT','GCC','GCA','GCG'],
"Thr": ['ACT','ACC','ACA','ACG'],
"Tyr": ['TAT','TAC'],
"His": ['CAT','CAC'],
"Gln": ['CAA','CAG'],
"Asn": ['AAT','AAC'],
"Lys": ['AAA','AAG'],
'Asp': ['GAT','GAC'],
"Glu": ['GAA','GAG'],
"Cys": ['TGT','TGC'],
"Trp": ['TGA','TGG'],
"Gly": ['GGT','GGC','GGA','GGG'],
"end(*)":['TAA','TAG','AGA','AGG'],
}

def degenerated_sites(codon):
# print(thisdict)

a_codon = list(codon)
b_codon = list(codon)
c_codon = list(codon)
nucleotides = ['A','C','T','G']
codons1=[]
codons2=[]
codons3=[]

for i in range(len(nucleotides)):
a_codon[0] = nucleotides[i]
first_site = "".join(a_codon)
codons1.append(first_site)
for i in range (len(nucleotides)):
b_codon[1] = nucleotides[i]
second_site = "".join(b_codon)
codons2.append(second_site)
for i in range (len(nucleotides)):
c_codon[2] = nucleotides[i]
third_site = "".join(c_codon)
codons3.append(third_site)
codons1.remove(codon)
codons2.remove(codon)
codons3.remove(codon)

for key, value in thisdict.items():
for i in range(len(codons1)):
if codons1[i] in value: # this is the magic line
print("key found!!!!", end=" ")
print(key)

degenerated_sites('CGT')

关于python - 用另一个列表遍历字典列表,并找到关联的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70993124/

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