gpt4 book ai didi

python - 遍历python中的列表

转载 作者:太空狗 更新时间:2023-10-30 01:12:37 26 4
gpt4 key购买 nike

我有一个序列列表 l(许多 1000 个序列):l = [ABCD,AABA,...]。我还有一个包含许多 4 字母序列(大约一百万个)的文件 f。我想为 f 中的每个序列选择 l 中最接近的字符串,汉明距离最多为 2,并更新计数器 good_count。我为此编写了以下代码,但速度很慢。我想知道是否可以更快地完成。

def hamming(s1, s2):
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))

f = open("input.txt","r")

l = [ABCD,AABA,...]

good_count = 0
for s in f:
x = f.readline()
dist_array = []
for ll in l:
dist = hamming(x,ll)
dist_array.append(dist)
min_dist = min(dist_array)
if min_dist <= 2:
good_count += 1
print good_count

如果 lf 很小,它工作得很快,但对于大的 lf 就需要很长时间。请提出一个更快的解决方案。

最佳答案

使用现有的库,例如 jellyfish:

from jellyfish import hamming_distance

这为您提供了汉明距离的 C 实现。

关于python - 遍历python中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41706120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com