[英]Python - DNA Transcription using a for loop
dictionary = {1: ['A', 'U'],
2: ['C', 'G'],
3: ['G', 'C'],
4: ['T', 'A']}
def transcribe(S):
"""Converts a single-character c from DNA
nucleotide to its complementary RNA nucleotide
"""
if S =='':
return ''
for i in dictionary:
S = S.replace(dictionary[i][0], dictionary[i][1])
return S
以上是我到目前为止的代码。 以下是我正在运行的测试。
print("Function 6 Tests")
print( "transcribe('ACGTTGCA') should be 'UGCAACGU' :", transcribe('ACGTTGCA') )
print( "transcribe('ACG TGCA') should be 'UGCACGU' :", transcribe('ACG TGCA') ) # Note that the space disappears
print( "transcribe('GATTACA') should be 'CUAAUGU' :", transcribe('GATTACA') )
print( "transcribe('cs5') should be '' :", transcribe('cs5') ) # Note that other characters disappear
print( "transcribe('') should be '' :", transcribe('') ) # Empty strings!
Function 6 Tests
transcribe('ACGTTGCA') should be 'UGCAACGU' : UCCAACCU
transcribe('ACG TGCA') should be 'UGCACGU' : UCC ACCU
transcribe('GATTACA') should be 'CUAAUGU' : CUAAUCU
transcribe('cs5') should be '' : cs5
transcribe('') should be '' :
以上是我得到的结果。
1)我不明白为什么即使我在字典中列出了 C 也不会转换为 G。 2)有没有办法修改第一个if语句,以便输入ATCG以外的任何其他内容都会导致打印''? 3)另外,我如何摆脱ACG和TGCA之间的空间?
考虑:
>>> a = "hello"
>>> a = a.replace('l', 'x')
>>> a
'hexxo'
>>> a = a.replace('x', 'l')
>>> a
'hello'
>>>
您有一个将 C 转换为 G 的条目,但是您有一个将 G 转换回 C 的条目。
尝试使用将字符映射到要替换的字符的字典:
d = {'A': 'U', 'C': 'G', 'G': 'C', 'T': 'A'}
现在您可以执行以下操作,您只需将每个字符转换一次。
>>> d = {'A': 'U', 'C': 'G', 'G': 'C', 'T': 'A'}
>>> d
{'A': 'U', 'C': 'G', 'T': 'A', 'G': 'C'}
>>> ''.join(d[ch] for ch in "ACTG")
'UGAC'
>>>
这假定您正在处理的字符串仅包含 A、C、G 或 T。
replace
替换所有实例。 问题在于 ACGTTGCA,有 2 个 C,因此一旦将 C 替换为 G,就将已经替换的 G 再次替换为 C。
使dictionary
成为从S
中的字母到替换字母的映射。 然后只需在循环中使用它来替换字母
# make the dictionary that maps the first list element to the second
d = {k:v for k,v in dictionary.values()}
def transcribe(S):
"""Converts a single-character c from DNA
nucleotide to its complementary RNA nucleotide
"""
if S =='':
return ''
# get dict values from S
return ''.join(d.get(k, '') for k in S)
也许值得考虑转向中间字母:
from typing import Dict, Final
DNA_2_RNA: Final[Dict[str, str]] = {
"A": "1",
"C": "2",
"G": "3",
"T": "4"
}
def transcribe(dna: str) -> str: # rna
temp = ""
for nucleotide in dna:
temp += DNA_2_RNA[nucleotide]
return (
temp
.replace("1", "U")
.replace("2", "G")
.replace("3", "C")
.replace("4", "A")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.