我有一组 n (~1000000) 个字符串(DNA 序列)存储在一个列表 trans 中。我必须找到列表中所有序列的最小汉明距离。我实现了一个幼稚的蛮力算法,已经运行了一天多,还没有给出解决方案。我的代码是
dmin=len(trans[0])
for i in xrange(len(trans)):
for j in xrange(i+1,len(trans)):
dist=hamdist(trans[i][:-1], trans[j][:-1])
if dist < dmin:
dmin = dist
有没有更有效的方法来做到这一点?这里 hamdist 是我编写的用于查找汉明距离的函数。这是
def hamdist(str1, str2):
diffs = 0
if len(str1) != len(str2):
return max(len(str1),len(str2))
for ch1, ch2 in zip(str1, str2):
if ch1 != ch2:
diffs += 1
return diffs