I created a function to calculate distance from every two places. x and y are a list of locations x s and y s
def calculate_distances(x,y):
N=len(str(x))
a=[[0]*N]*N
for i,j in range(N):
a[i][j]=np.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
return a
I applied it in a segment that offered by instructor.
D = calculate_distances(x,y)
fig = plt.figure(figsize=(6,6));
total_distance = 0
for i in range(n_city-1):
plt.scatter(x,y,marker="s",c="k");
plt.plot([x[i],x[i+1]], [y[i],y[i+1]],
alpha=(i+1)/(n_city),lw=2,color="k");
total_distance += D[i,i+1]
plt.title("Distance traveled = %0.3f" %total_distance)
time.sleep(1.0)
clear_output(wait = True)
display(fig) # Reset display
And I received the error msg, I pasted below
TypeError Traceback (most recent call last)
<ipython-input-21-3804c182182f> in <module>
3 # We use the calculate_distances function you created above to compute the distance matrix
4 # Make sure you feed in the correct "x" and "y" arrays if you used different variables names
----> 5 D = calculate_distances(x,y)
6
7 fig = plt.figure(figsize=(6,6));
<ipython-input-20-ecc344ee386f> in calculate_distances(x, y)
3 N=len(str(x))
4 a=[[0]*N]*N
----> 5 for i,j in range(N):
6 a[i][j]=np.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
7 return a
TypeError: cannot unpack non-iterable int object```