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

Iterating through dictionary lists with another list, and finding associated key of value

我正在编写代码,通过与以下 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

在以下行:

if value == codons1[i]:

您似乎是在将单个密码子与密码子列表进行比较,因此您永远无法找到匹配项。

试试看:

if codons1[i] in value:

我试过了,似乎有效

Ser
Cys
Gly

您需要检查密码子列表中的项目是否是 in 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')