我想写一个函数,它接受一长串字符(一个像‘ugguuauuaugguuaaugguuu’这样的蛋白质序列),并一次从中提取三个字符(即密码子)。它可以依次返回每组三个字符,也可以返回包含所有三个字符的列表。两种方法都可以。但我在弄清楚如何干净地完成这件事上遇到了一些困难
以下是我目前掌握的情况:
def get_codon_list(codon_string):
codon_start = 0
codon_length = 3
codon_end = 3
codon_list = []
for x in range(len(codon_string) // codon_length):
codon_list.append(codon_string[codon_start:codon_end])
codon_start += codon_length
codon_end += codon_length
return codon_list
它可以返回密码子列表,但似乎效率很低。如果有更好的方法,我不喜欢使用硬编码的数字和递增的变量。我也不喜欢使用for循环,因为它实际上不使用循环中的变量。这似乎不是一个正确的使用它
有没有关于如何改进这一点的建议,或者是使用特定的函数/模块,或者只是使用更好的Pythonic技术
谢谢
您可以使用列表理解,每次从字符串中获取长度为3的片段
{}石斑鱼配方非常适合({a1}):
只需使用^{} 函数的
step
参数即可避免维护变量:然后它可以变成一个列表:
相关问题 更多 >
编程相关推荐