【Python】各类距离公式

版权声明:非技术文章禁止转载,如有需要请私信作者。技术类文章欢迎转载,转载请注明出处: http://blog.csdn.net/ztf312/ https://blog.csdn.net/ztf312/article/details/84110705

汉明距离

def hammingDistance(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(el1 != el2 for el1, el2 in zip(s1, s2))

曼哈顿距离

def manhattanDistance(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(map(lambda i, j:abs(i-j), s1, s2))

猜你喜欢

转载自blog.csdn.net/ztf312/article/details/84110705
今日推荐