我目前正在编写一个分析偏斜差异的脚本。不幸的是,我的问题是当字符串的长度增加时,运行时间变得太长,我似乎无法计算出我的答案。
def SkewGC(file):
countG = 0
countC = 0
diffGtoC = ""
# first, we need to find number of G's.
# the idea is, if G appears, we add it to the count.
# We'll just do the same to each one.
for pos in range(0,len(file)):
if file[pos] == "G":
countG = countG+1
if file[pos] == "C":
countC = countC+1
diffGtoC = diffGtoC + str(countG-countC) + ","
return diffGtoC.split(",")
SkewGCArray = SkewGC(data)
# This because I included extra "," at the end...
SkewGCArray = [int(i) for i in SkewGCArray[:len(SkewGCArray)-1]]
def min_locator(file):
min_indices = ""
for pos in range(0,len(file)):
if file[pos] == min(file):
min_indices = min_indices + str(pos) + " "
return min_indices
print min_locator(SkewGCArray)
本质上,这个脚本计算 G 和 C 的数量(对应于 DNA 中的核苷酸),获得每个位置的差异,然后我试图找到最小值的索引。它适用于低长度的文件(即输入字符串),但是当长度变大时 - 即使像 90000+,然后我的脚本运行但无法在合理的时间内(约 4-5 分钟)解决答案。
谁能指出我可以做些什么来让它更快?我考虑过是否最好说,获得差异(diffGtoC),将其设置为最小值,然后重新计算每个差异,直到它看到不同的东西,在此期间我也替换最小值。
但是我对这种方法的担忧是寻找和保留最小值的索引。如果我说,有一个包含值的数组:
[-4,-2,-5,-6,-5,-6]
我可以看到更改最小值(-4 到 -5,然后到 -6)在算法运行时方面会更快,但我如何能够保持 -6 的位置?不确定这是否完全有意义。