汉明距离是计算两个等长字符串之间的差异性的度量。如果你需要使用汉明距离来比较两个字符串,请参考以下示例代码:
def hamming_distance(str1, str2):
if len(str1) != len(str2):
raise ValueError("Strings must have the same length.")
return sum(ch1 != ch2 for ch1, ch2 in zip(str1, str2))
if __name__ == "__main__":
str1 = "110011001100"
str2 = "101010101010"
distance = hamming_distance(str1, str2)
print(f"Hamming distance between '{str1}' and '{str2}' is {distance}.")