我正在尝试修复下面的代码。
from pylab import *
protein = ''
amino_acids = ['P', 'G', 'L', 'A']
while ________:
protein += choice(amino_acids)
if len(protein) == 1000:
print(protein)
我想要得到的结果是一个长度为 1000 的随机蛋白质,由四个给定的氨基酸(P、G、L 和 A)组成。怎么了?
最佳答案
一个可能的解决方案(除其他外,可能更Pythonic)是
from pylab import *
protein = ''
amino_acids = ['P', 'G', 'L', 'A']
while len(protein) <= 1000:
protein = protein + choice(amino_acids)
print(protein)
在这里,您创建一个空字符串蛋白质
。 while
循环启动并附加来自 amino_acids
的随机元素,直到达到字符串长度 1000。
一句台词是
proteins = ''.join([choice(amino_acids) for x in range(1000)])
这里有一个所谓的 list comprehension用于从 amino_acids
生成 1000 个选择。这 1000 个元素存储为列表。应用 join
将元素折叠为单个字符串。
请注意,''
表示我们不需要氨基酸之间有分隔符。使用例如'-'
相反会导致类似
L-A-P-G-L-...
关于python - 修复 python/Bioinformatics 中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47163992/