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 - when using the cv2adaptivethreshold function in the binarization process of an image, an error occurs in medianblur and
- how to avoid errors in python explode function
- [python] does the function exp work for matrices?
- [python] i'm worried that i can't write smartly the code that returns the data that deviates from the distribution consisting of
- i want to do something like a function pointer in a class in python
- python - get right-justified dataframe with split function of pandas dataframe
- python - split function in pandas dataframe
- python - i want to know how this function is recursively defined
- python - about the difference in behavior between sort and sorted
- python - regarding the difference between 127001 and localhost
- creating a function that gives the standard deviation in python without using numpy
- python 3x - exchange of values between classes
- python - i want to define a function using a csv file
- python - in machine learning learning, the accuracy is 100% and the value of the loss function is almost 0 from the beginning an
- python - about function calls
- i want to create a function to judge python prime numbers and display 15 prime numbers of integer n or more
- print function arguments sep, end, etc do not work in python
- python 3x - i want to use python3 tkinter drag and drop function in an exe file
- can multiple file io by built-in function open using multithreading in python be expected to be faster?
- i want to put the result of a function in a dataframe in python
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- i want to call a child component method from a parent in vuejs