我已经实现了一个函数。对于图片中的每一列,它取最常用的元素并从该列中的元素总数中减去该元素。然后把这些数字加起来。 This image shows what the function does.
有没有办法让它更快? 这是我的代码:
def scoreMotifs(motifs):
'''This function computes the score of list of motifs'''
z = []
for i in range(len(motifs[0])):
y = ''
for j in range(len(motifs)):
y += motifs[j][i]
z.append(y)
print z
totalscore = 0
for string in z:
score = len(string)-max([string.count('A'),string.count('C'), string.count('G'), string.count('T')])
totalscore += score
return totalscore
motifs = ['GCG','AAG','AAG','ACG','CAA']
scoreMotifs(motifs)
['GAAAC', 'CAACA', 'GGGGA']
5
好的,所以我用line_profiler来分析你的代码:
结果如下:
^{pr2}$有大量的计算:
不过,还有一种更好的方法来转置字符串,使用
zip
技巧。因此,您可以将代码重写为:总时间:
我认为这是一个相当不错的进步。在
相关问题 更多 >
编程相关推荐