繁体   English   中英

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

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

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

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

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

现在,我的目标是对照该字典检查每个修改列表的元素,并查看它们对应的 dict 键。 我希望看看初始元素的变化是否会改变与之关联的结果键。

我无法弄清楚如何进行; 如何获取修改列表值的对应键?

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

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字典的值中,字典是一个列表。 所以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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM