×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

How to calculate hamming distance in Python?

By Shivang Yadav Last updated : November 21, 2023

Hamming Distance

Hamming distance is a measure of the difference between two vectors. It is defined as the number of positions at which the corresponding elements.

Calculation of Hamming Distance

Python provides a built-in method hamming() present in the scipy library. The function returns the percentage of corresponding elements that differ between the two given vector arrays.

Python program to calculate hamming distance

# Program to calculate Hamming distance
from scipy.spatial.distance import hamming

vecA = [1, 4, 6, 8, 9]
vecB = [3, 4, 6, 2, 7]

print("Vector A", vecA)
print("Vector B", vecB)

ham = hamming(vecA, vecB) * len(vecA)

print("Hamming Distance Between vectors", ham)

Output

The output of the above program is:

Vector A [1, 4, 6, 8, 9]
Vector B [3, 4, 6, 2, 7]
Hamming Distance Between vectors 3.0

Python SciPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.