I have a problem and would like your help. I have a class (Customers) in which all customers are stored with their respective x and y coordinates. I would now like to build a distance matrix or a two dimensional list that shows the distances between customers.
I already have a working function for the distances:
# Getting Distances
def get_distance(a,b):
d = [a.getX()- b.getX() , a.getY() - b.getY()]
return sqrt(d[0] * d[0] + d[1] * d[1])
# Distances between Customers:
Distances = []
for i in range(0,nr_customers):
for j in range(0, nr_customers):
get_distance(customer[i],customer[j])
Distances.append(get_distance(customer[i],customer[j]))
So far, of course, only one list of all distances is output. Can someone help me? Please without Numpy or Pandas, we do not allow their use.
What you are missing in your code is the distance of customer i with all other customers(including i). You are not storing them in the inner loop as a list. You need to define it in your outer loop.
Now you have the distances between all customers as a 2D list.
Although you could get the same result using scipy
distance_matrix():