将字典中特定键添加到列表中?

huangapple go评论74阅读模式
英文:

How can I add specific keys from a dictionary to a list?

问题

这是程序的工作部分:

dna_sequence = "GAGCGTCTGCTCCGTGTATAAGCCACGTCGGAGCT"
codons = [dna_sequence[i:i+3] for i in range (0, len(dna_sequence), 3)]

print("codons")
print(codons)


genetic_code = {
    "A": {"GCG" :33,"GCA" :23,"GCT" :18,"GCC" :26},       
    "R": {"AGG" :3,"AGA" :6, "CGG" :30, "CGA" :6,"CGT" :28,"CGC" :28},
    "N": {"AAT" :50,"AAC" :50},
    "D": {"GAT" :63, "GAC":27},
    "C": {"TGT" :46,"TGC" :54},
    "*": {"TGA" :25,"TAG" :25,"TAA" :50}, # * Stop codon
    "Q": {"CAG" :65,"CAA" :35},
    "E": {"GAG" :33,"GAA" :67},
    "G": {"GGG" :15,"GGA" :15,"GGT" :35,"GGC" :36},
    "H": {"CAT" :60,"CAC" :40},
    "I": {"ATA" :11,"ATT" :50,"ATC" :39},
    "L": {"TTG" :13,"TTA" :14,"CTG" :48,"CTA" :4,"CTT" :12,"CTC" :10},
    "K": {"AAG" :24,"AAA" :76},
    "M": {"ATG" :100}, # Start codon
    "F": {"TTT" :58,"TTC" :42},
    "P": {"CCG" :50,"CCA" :21,"CCT" :17,"CCC" :12},
    "S": {"AGT" :16,"AGC" :24,"TCG" :15,"TCA" :15,"TCT" :16,"TCC" :15},
    "T": {"ACG" :25,"ACA" :16,"ACT" :19,"ACC" :40},
    "W": {"TGG" :100},
    "Y": {"TAT" :59,"TAC" :41},
    "V": {"GTG" :35,"GTA" :17,"GTT" :28,"GTC" :20}
}

codonValue = []

for codon in codons:
    for AS in genetic_code:
        if codon in genetic_code[AS]:
            codonValue.append(genetic_code[AS][codon]) 

print("codonValue")
print(codonValue)


CodonArray = list(zip(codons, codonValue))

print("CodonArray")
print(CodonArray)

在这个程序中,我尝试将氨基酸整合到CodonArray中:

codonValue = []     # empty list codon value
ASseq = []          # empty list amino acid sequence

for codon in codons:
    for AS in genetic_code:
        if codon in genetic_code[AS]:
            codonValue.append(genetic_code[AS][codon])    

for codon in codons:         
    if codon in genetic_code[AS]:
        ASseq.append(list(genetic_code.keys()))


print("codonValue")
print(codonValue)
print("ASseq")
print(ASseq)

CodonArray = list(zip(codons, codonValue, ASseq))

print("CodonArray")
print(CodonArray)

显然,这没有起作用。

英文:

I want to build a code that splits a DNA sequence in its codon triplets and store them in a list, than use these strings find the relative frequency of these codons within the human organism. Finally the programm should arrange the codon triplets with the according frequency in an array. So far the program is working.
Now I want to add a third element to the array, and that is the amino acid the triplet encodes. In my dictionary, that are the keys.
Is there a proper way to get this specific keys used for the generation of the codon_value list out of the same dictonary or do I need to open up a second dictionary with the codons as keys and the respective amino acid as value?

This is the working part of the program:

dna_sequence = "GAGCGTCTGCTCCGTGTATAAGCCACGTCGGAGCT"
codons = [dna_sequence[i:i+3] for i in range (0, len(dna_sequence), 3)]
print("codons")
print(codons)
genetic_code = {
"A": {"GCG" :33,"GCA" :23,"GCT" :18,"GCC" :26},       
"R": {"AGG" :3,"AGA" :6, "CGG" :30, "CGA" :6,"CGT" :28,"CGC" :28},
"N": {"AAT" :50,"AAC" :50},
"D": {"GAT" :63, "GAC":27},
"C": {"TGT" :46,"TGC" :54},
"*": {"TGA" :25,"TAG" :25,"TAA" :50}, # * Stop codon
"Q": {"CAG" :65,"CAA" :35},
"E": {"GAG" :33,"GAA" :67},
"G": {"GGG" :15,"GGA" :15,"GGT" :35,"GGC" :36},
"H": {"CAT" :60,"CAC" :40},
"I": {"ATA" :11,"ATT" :50,"ATC" :39},
"L": {"TTG" :13,"TTA" :14,"CTG" :48,"CTA" :4,"CTT" :12,"CTC" :10},
"K": {"AAG" :24,"AAA" :76},
"M": {"ATG" :100}, # Start codon
"F": {"TTT" :58,"TTC" :42},
"P": {"CCG" :50,"CCA" :21,"CCT" :17,"CCC" :12},
"S": {"AGT" :16,"AGC" :24,"TCG" :15,"TCA" :15,"TCT" :16,"TCC" :15},
"T": {"ACG" :25,"ACA" :16,"ACT" :19,"ACC" :40},
"W": {"TGG" :100},
"Y": {"TAT" :59,"TAC" :41},
"V": {"GTG" :35,"GTA" :17,"GTT" :28,"GTC" :20}
}
codonValue = []
for codon in codons:
for AS in genetic_code:
if codon in genetic_code[AS]:
codonValue.append(genetic_code[AS][codon]) 
print("codonValue")
print(codonValue)
CodonArray = list(zip(codons, codonValue))
print("CodonArray")
print(CodonArray)
Output:
codons
['GAG', 'CGT', 'CTG', 'CTC', 'CGT', 'GTA', 'TAA', 'GCC', 'ACG', 'TCG', 'GAG', 'CT']
codonValue
[33, 28, 48, 10, 28, 17, 50, 26, 25, 15, 33]
CodonArray
[('GAG', 33), ('CGT', 28), ('CTG', 48), ('CTC', 10), ('CGT', 28), ('GTA', 17), ('TAA', 50), ('GCC', 26), ('ACG', 25), ('TCG', 15), ('GAG', 33)]

And in this program I tried to integrate the amino acids within the CodonArray:

dna_sequence = "GAGCGTCTGCTCCGTGTATAAGCCACGTCGGAGCT"
codons = [dna_sequence[i:i+3] for i in range (0, len(dna_sequence), 3)]
print("codons")
print(codons)
genetic_code = {
"A": {"GCG" :33,"GCA" :23,"GCT" :18,"GCC" :26},        
"R": {"AGG" :3,"AGA" :6, "CGG" :30, "CGA" :6,"CGT" :28,"CGC" :28},
"N": {"AAT" :50,"AAC" :50},
"D": {"GAT" :63, "GAC":27},
"C": {"TGT" :46,"TGC" :54},
"*": {"TGA" :25,"TAG" :25,"TAA" :50}, # * Stop codon
"Q": {"CAG" :65,"CAA" :35},
"E": {"GAG" :33,"GAA" :67},
"G": {"GGG" :15,"GGA" :15,"GGT" :35,"GGC" :36},
"H": {"CAT" :60,"CAC" :40},
"I": {"ATA" :11,"ATT" :50,"ATC" :39},
"L": {"TTG" :13,"TTA" :14,"CTG" :48,"CTA" :4,"CTT" :12,"CTC" :10},
"K": {"AAG" :24,"AAA" :76},
"M": {"ATG" :100}, # Start codon
"F": {"TTT" :58,"TTC" :42},
"P": {"CCG" :50,"CCA" :21,"CCT" :17,"CCC" :12},
"S": {"AGT" :16,"AGC" :24,"TCG" :15,"TCA" :15,"TCT" :16,"TCC" :15},
"T": {"ACG" :25,"ACA" :16,"ACT" :19,"ACC" :40},
"W": {"TGG" :100},
"Y": {"TAT" :59,"TAC" :41},
"V": {"GTG" :35,"GTA" :17,"GTT" :28,"GTC" :20}
}
#genetic_code["A"] -> spuckt zeile aus
#genetic_code["A"]["GCG"]: spuckt 33 aus
codonValue = []     #empty list codon value
ASseq = []          #empty list aminoacid sequence
for codon in codons:
for AS in genetic_code:
if codon in genetic_code[AS]:
codonValue.append(genetic_code[AS][codon])    
for codon in codons:         
if codon in genetic_code[AS]:
ASseq.append(genetic_code.keys())
print("codonValue")
print(codonValue)
print("ASseq")
print(ASseq)
CodonArray = list(zip(codons, codonValue, ASseq))
print("CodonArray")
print(CodonArray)

Obviously, that didn't work.

codons
['GAG', 'CGT', 'CTG', 'CTC', 'CGT', 'GTA', 'TAA', 'GCC', 'ACG', 'TCG', 'GAG', 'CT']
codonValue
[33, 28, 48, 10, 28, 17, 50, 26, 25, 15, 33]
ASseq
[dict_keys(['A', 'R', 'N', 'D', 'C', '*', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V'])]
CodonArray
[('GAG', 33, dict_keys(['A', 'R', 'N', 'D', 'C', '*', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V']))]

Any suggestions how to solve this problem?
Thanks in advance 将字典中特定键添加到列表中?

答案1

得分: 2

In if codon in genetic_code[AS]:, the variable AS is out of scope because its value is not updated inside this loop, the problem is that you are always checking against the last value of AS from the previous loop.

if codon in genetic_code[AS]: 这行代码中,变量 AS 是超出作用域的,因为它在这个循环内没有更新它的值,问题是你总是检查前一个循环中 AS 的最后一个值。

Let's try to update the amino acid in the first loop when you are appending the codon value.

让我们尝试在第一个循环中更新氨基酸,在您附加密码子值时。

dna_sequence = "GAGCGTCTGCTCCGTGTATAAGCCACGTCGGAGCT"
codons = [dna_sequence[i:i+3] for i in range(0, len(dna_sequence), 3)]

print("codons")
print(codons)

genetic_code = {
    "A": {"GCG" :33,"GCA" :23,"GCT" :18,"GCC" :26},
    # ... (other genetic code entries)
}

codonValue = []     # empty list codon value
ASseq = []          # empty list amino acid sequence

for codon in codons:
    found = False  # flag to check if codon was found
    for AS in genetic_code:
        if codon in genetic_code[AS]:
            codonValue.append(genetic_code[AS][codon])
            ASseq.append(AS)  # append the amino acid to ASseq
            found = True  # set the flag to True if codon was found
            break  # exit the inner loop once found
    if not found:
        # handle the case when the codon is not found in genetic code
        codonValue.append("Not found")
        ASseq.append("Not found")

print("codonValue")
print(codonValue)
print("ASseq")
print(ASseq)

CodonArray = list(zip(codons, codonValue, ASseq))

print("CodonArray")
print(CodonArray)

This code snippet adds a found flag to check if the codon was found in the genetic code and breaks out of the inner loop when found. It also handles the case when the codon is not found in the genetic code.

英文:

In if codon in genetic_code[AS]:, the variable AS is out of scope because its value is not updated inside this loop, the problem is that you are always checking against the last value of AS from the previous loop.

Lets try to update the amino acid in the first loop when you are appending the codon value

dna_sequence = "GAGCGTCTGCTCCGTGTATAAGCCACGTCGGAGCT"
codons = [dna_sequence[i:i+3] for i in range (0, len(dna_sequence), 3)]
print("codons")
print(codons)
genetic_code = {
"A": {"GCG" :33,"GCA" :23,"GCT" :18,"GCC" :26},        
"R": {"AGG" :3,"AGA" :6, "CGG" :30, "CGA" :6,"CGT" :28,"CGC" :28},
"N": {"AAT" :50,"AAC" :50},
"D": {"GAT" :63, "GAC":27},
"C": {"TGT" :46,"TGC" :54},
"*": {"TGA" :25,"TAG" :25,"TAA" :50}, # * Stop codon
"Q": {"CAG" :65,"CAA" :35},
"E": {"GAG" :33,"GAA" :67},
"G": {"GGG" :15,"GGA" :15,"GGT" :35,"GGC" :36},
"H": {"CAT" :60,"CAC" :40},
"I": {"ATA" :11,"ATT" :50,"ATC" :39},
"L": {"TTG" :13,"TTA" :14,"CTG" :48,"CTA" :4,"CTT" :12,"CTC" :10},
"K": {"AAG" :24,"AAA" :76},
"M": {"ATG" :100}, # Start codon
"F": {"TTT" :58,"TTC" :42},
"P": {"CCG" :50,"CCA" :21,"CCT" :17,"CCC" :12},
"S": {"AGT" :16,"AGC" :24,"TCG" :15,"TCA" :15,"TCT" :16,"TCC" :15},
"T": {"ACG" :25,"ACA" :16,"ACT" :19,"ACC" :40},
"W": {"TGG" :100},
"Y": {"TAT" :59,"TAC" :41},
"V": {"GTG" :35,"GTA" :17,"GTT" :28,"GTC" :20}
}
codonValue = []     #empty list codon value
ASseq = []          #empty list aminoacid sequence
for codon in codons:
for AS in genetic_code:
if codon in genetic_code[AS]:
codonValue.append(genetic_code[AS][codon])  
ASseq.append(AS)  # append the amino acid to ASseq
print("codonValue")
print(codonValue)
print("ASseq")
print(ASseq)
CodonArray = list(zip(codons, codonValue, ASseq))
print("CodonArray")
print(CodonArray)

huangapple
  • 本文由 发表于 2023年5月13日 19:16:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定