使用另外一个列表迭代字典列表,并找到对应值的键

3

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

我已经创建了3个列表,其中初始元素的第1、2或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

4
你可以从独立于生物应用的角度来解释这个问题,这样可能会更有益。请记住,大多数用户可能无法理解你所描述的问题的某些部分,但是他们可以理解“我有一个字符串映射到列表的字典,我需要根据某些特征提取这些内容”的意思。 - 0x263A
@0x263A 这是完全公平的,这是我试图尽可能详细地描述问题(并可能希望生物信息学专家能够指导我找到最适合我情况的解决方案)所做的事情。我会相应地更新问题。 - PWier
2个回答

1

您需要检查密码子列表中的项是否在字典的值(一个列表)中。因此,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')

使用这种方法对我来说效果很好! - PWier

0

在以下行:

if value == codons1[i]:

看起来你正在将单个密码子与密码子列表进行比较,因此永远不会得到匹配。

尝试改为:

if codons1[i] in value:

我尝试了这个,似乎有效

Ser
Cys
Gly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接