A function that returns the distance between an n-dimensional vector x and y, and a basic function that returns a Euclidean distance. In this case, I would like to write the most foolishly without using a library such as numpy. Specifically, this code calculates the Euclidean distance of a 3D vector.Error message
The order was reversed, but I wrote it in "I tried it".
Applicable source code
No error message.
python3 x = [-2, 1, 6] y = [3, -1, -2] def ex4_4 (x, y): d = 0 for i in range (0, len (x)): for j in range (0, len (y)): t = 0 t = t + (x [i]-y [j]) ** 2 d = t ** (1/2) return d
In jupyter notebook
When ex4_4 (x, y) is executed, the result is 8 and it is assumed that only the last number in the list x and y is committed. I don't know why. Why isn't t added?
Please provide more detailed information here.
Answer # 1
In addition to hayataka2049's indication,
It is a double loop, but it is not necessary to make a double loop because it only calculates the distance.
The code after correction is written.
x = [-2, 1, 6] y = [3, -1, -2] def ex4_4 (x, y): t = 0 for i in range (0, len (x)): t = t + (x [i]-y [i]) ** 2 return t ** (0.5) print (ex4_4 (x, y))
The execution results are as follows
I hope you find it helpful.
Answer # 2
T = 0 is written inside the loop.
- python 3x - calculation method of distance between data using log-likelihood
- python - i want to add an article category sorting function on the django blog
- python 3x - difference between your own code and the correct code
- python - what is the difference between the two jupyter kernels generated by conda?
- processing using the len function when an integer value is obtained from python standard input
- python - i want to create a function that finds the standard deviation sigma from x, y
- please explain the function using the python dictionary
- python - i want to get rid of the error that the function is defined but not defined
- python - i'm looking for a method like the countif function in excel for pywin32
- python - i want to know the type of softmax function in pytorch
- python - pass a variable to the websocket on_message function
- python - arguments are passed even though the function inside the function is not in parentheses
- php - [creating a function that returns two values with random numbers assigned]
- python 3x - i want to shorten the python function
- python - when using the cv2adaptivethreshold function in the binarization process of an image, an error occurs in medianblur and
- php creation of a function that returns two values with random numbers assigned
- i want to create a function in python that all functions without class go through
- python - out of range of predict function used in image recognition (?)
- python - array elements cannot be used as function arguments ??