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

标签 python list dictionary iteration bioinformatics

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

相关文章:

python - 在 PyCharm 之外运行 PyCharm 项目

python - 如何使用 'os' 从 'with' 打开文件对象?

Python:循环匹配某个值的列表列表并更改它

Python 检查项目是否在不同的集合中,并将它们合并

python - 在字典中附加列表的最佳方法是什么?

python - 在 Python 中,使用二分法在字典列表中查找项目

python - 打开 Spyder 时不打开 IPython 控制台

python - 带有字符串 X 和 Y 坐标的散点图

python - append 到 pandas 数据框中的列表

python - 如何按值过滤字典?