To calculate a distance matrix between two cities in Python, you can use the geopy library that provides different distance functions. Here's an example code snippet to calculate the distance matrix:
main.py516 chars18 lines
Explanation:
geopy to calculate the distance and numpy to create a matrix.(latitude, longitude) format. In the example, we use New York City and London.geodesic() function to calculate the distance between the two cities in km.geodesic(). The resulting matrix is printed.Note that you can replace the latitude and longitude coordinates with your desired cities.
gistlibby LogSnag