gpt4 book ai didi

python - 将输入代码拆分为 3 个字母并使用 if 语句返回 DNA->氨基酸字母

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:12 25 4
gpt4 key购买 nike

它正在按预期读取前 3 个字母,但我如何让它读取接下来的 3 个字母作为不同的字符串,以便它可以再次运行 if、elif、else 语句。

else X 在那里是因为它不需要我做其余的。

该表之所以存在,只是因为它有助于查看它是否返回了正确的字母。

dna = input("Enter the DNA sequence to translate: ")

if dna == "ATA" or dna == "ATC" or dna == "ATT":
print ("I")
elif dna == "CTA" or dna == "CTC" or dna == "CTG" or dna == "CTT" or dna == "TAA" or dna =="TTG":
print ("L")
elif dna == "GTA" or dna == "GTC" or dna == "GTG" or dna == "GTT":
print ("V")
elif dna == "TTC" or dna == "TTT":
print ("F")
elif dna == "ATG":
print ("M")
else:
print ("X")


CodonDict = {
'ATT':'I', 'ATC':'I', 'ATA':'I', 'CTT':'L', 'CTC':'L',
'CTA':'L', 'CTG':'L', 'TTA':'L', 'TTG':'L', 'GTT':'V', 'GTC':'V',
'GTA':'V', 'GTG':'V', 'TTT':'F', 'TTC':'F', 'ATG':'M', 'TGT':'C',
'TGC':'C', 'GCT':'A', 'GCC':'A', 'GCA':'A', 'GCG':'A', 'GGT':'G',
'GGC':'G', 'GGA':'G', 'GGG':'G', 'CCT':'P', 'CCC':'P', 'CCA':'P',
'CCG':'P', 'ACT':'T', 'ACC':'T', 'ACA':'T', 'ACG':'T', 'TCT':'S',
'TCC':'S', 'TCA':'S', 'TCG':'S', 'AGT':'S', 'AGC':'S', 'TAT':'Y',
'TAC':'Y', 'TGG':'W', 'CAA':'Q', 'CAG':'Q', 'AAT':'N', 'AAC':'N',
'CAT':'H', 'CAC':'H', 'GAA':'E', 'GAG':'E', 'GAT':'D', 'GAC':'D',
'AAA':'K', 'AAG':'K', 'CGT':'R', 'CGC':'R', 'CGA':'R', 'CGG':'R',
'AGA':'R', 'AGG':'R', 'TAA':'X', 'TAG':'X', 'TGA':'X'}

For example I input: ATT
Returns I as expected.

I input: ATTATT
Returns X as expected but how do I treat the 3 letters as separate.
It should return II.

最佳答案

例子:

input = 'ATTATTTTAGGG'
for i in range(0,len(intput),3):
print (input[i:i+3])

输出:

ATT
ATT
TTA
GGG

Accessing Values in Strings Python does not support a character type; these are treated as strings of length one, thus also considered a substring.

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −

var1 = 'Hello World!'  
var2 = "Python Programming"

print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])

output:

var1[0]:  H  
var2[1:5]: ytho

方案一:

dna_ = input("Enter the DNA sequence to translate: ")
for i in range(0,len(dna_),3):
#print (dna_[i:i+3])
dna = dna_[i:i+3]
if dna == "ATA" or dna == "ATC" or dna == "ATT":
print ("I")
elif dna == "CTA" or dna == "CTC" or dna == "CTG" or dna == "CTT" or dna == "TAA" or dna =="TTG":
print ("L")
elif dna == "GTA" or dna == "GTC" or dna == "GTG" or dna == "GTT":
print ("V")
elif dna == "TTC" or dna == "TTT":
print ("F")
elif dna == "ATG":
print ("M")
else:
print ("X")

解决方案2:(如果你想使用现有的字典)

dna_ = input("Enter the DNA sequence to translate: ")
for i in range(0,len(dna_),3):
#print (dna_[i:i+3])
dna = dna_[i:i+3]
for key, val in CodonDict.items():
if key == dna:
print (val)

输出:

Enter the DNA sequence to translate: TGCCTG
C
L

关于python - 将输入代码拆分为 3 个字母并使用 if 语句返回 DNA->氨基酸字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com