如何阻止代码重复输出,下面是代码:
def get_complement(str):
'''
(str) -> str
Return the nucleotide's complement.
>>>get_complement('ATT')
'TAA'
>>>get_complement('CGCC')
'GCGG'
>>>get_complement('ATG')
'TAC'
>>>get_complement('TCAG')
'AGTC'
>>>get_complement('ATCG')
'TAGC'
'''
for i in str:
if (i == 'A'):print('T', end = "")
elif (i == 'T'):print('A', end = "")
elif (i == 'C'):print('G', end = "")
elif (i == 'G'):print('C', end = "")
print(get_complement(str))发布于 2022-04-17 15:40:24
如果我正确理解你的问题,你应该这样做:
nucleotide_complement_map = {
"A": "T",
"T": "A",
"C": "G",
"G": "C"
}
def get_complement(s):
'''
(str) -> str
Return the nucleotide's complement.
>>>get_complement('ATT')
'TAA'
>>>get_complement('CGCC')
'GCGG'
>>>get_complement('ATG')
'TAC'
>>>get_complement('TCAG')
'AGTC'
>>>get_complement('ATCG')
'TAGC'
'''
return "".join([nucleotide_complement_map[c] for c in s])
s = 'ATT'
print(get_complement(s))我还更改了参数"str“的名称,因为它是python中内置的名称,不应该用作参数名。
https://stackoverflow.com/questions/71903124
复制相似问题