擅长:python、mysql、java
<p>我会使用<a href="https://docs.python.org/2/library/functools.html#functools.reduce" rel="nofollow">^{<cd1>}</a>:</p>
<pre><code>from functools import reduce
def hamming_distance(s1, s2):
#Return the Hamming distance between equal-length sequences
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))
if __name__ == '__main__':
with open('h1.txt') as f:
f1 = f.read().splitlines()
with open('h2.txt') as f:
f2 = f.read().splitlines()
for line in f1:
print(line, reduce(lambda x, y: x if hamming_distance(line, y) > hamming_distance(line, x) else y, f2))
</code></pre>
<p>输出:</p>
^{pr2}$