我正在尝试编写一个函数,该函数接受一串 DNA 并返回恭维。我已经尝试解决这个问题一段时间了,并查看了 Python 文档,但无法解决。我已经为函数编写了文档字符串,因此您可以看到答案应该是什么样子。我在这个论坛上看到过类似的问题,但我无法理解答案。如果有人可以仅使用 str 格式和循环/if 语句来解释这一点,我将不胜感激,因为我还没有详细研究过字典/列表。
我试过str.replace但无法让它适用于多个元素,尝试使用嵌套的 if 语句,但这也不起作用。然后我尝试编写 4 个单独的 for 循环,但无济于事。
def get_complementary_sequence(dna):
""" (str) -> str
Return the DNA sequence that is complementary
to the given DNA sequence.
>>> get_complementary_sequence('AT')
TA
>>> get_complementary_sequence('GCTTAA')
CGAATT
"""
for char in dna:
if char == A:
dna = dna.replace('A', 'T')
elif char == T:
dna = dna.replace('T', 'A')
# ...and so on
Run Code Online (Sandbox Code Playgroud) 你好,我正在学习 python,我对 python for 循环的计数感到困惑。
我正在实现函数make_str_from_column:这会从列表列表中创建一个字符串,表示一列。
我不明白的是,当我将计数设置为 0 时,我收到一条错误消息。当我将计数设置为 -1 时它有效吗?将不胜感激您的反馈,非常感谢。
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
File "/Users/<name>/Downloads/a3.py", line 77, in make_str_from_column
new_element = board[count][column_index]
IndexError: list index out of range
>>>
Run Code Online (Sandbox Code Playgroud)
def make_str_from_column(board, column_index):
""" (list of list of str, int) -> str
Return the characters from the column of the board with index column_index
as a single string.
>>> …Run Code Online (Sandbox Code Playgroud)