-2

I've been trying to get mediana from this list which means the list that has shortest Euclidean distance.

I have made a function euclid that returns the distance between 2 vectors regardless of their size, but I have a problem with 2 for loops.

This program should return [1,2,3]

xs=[[1, 1, 1], [3, 2, 1], [1, 0, 3], [1, 2, 3], [4, 4, 4]]
naj = 0
vsota=0
ys=[]
for i,j in enumerate(xs):
    for x,y in enumerate(xs):
        if j!=y:
            vsota=euclid(j,y)

            print(vsota,"            ",j,y)

but it currently returns:

2.23606797749979                [1, 1, 1] [3, 2, 1]
2.23606797749979                [1, 1, 1] [1, 0, 3]
2.23606797749979                [1, 1, 1] [1, 2, 3]
5.196152422706632               [1, 1, 1] [4, 4, 4]
2.23606797749979                [3, 2, 1] [1, 1, 1]
3.4641016151377544              [3, 2, 1] [1, 0, 3]
2.8284271247461903              [3, 2, 1] [1, 2, 3]
3.7416573867739413              [3, 2, 1] [4, 4, 4]
2.23606797749979                [1, 0, 3] [1, 1, 1]
3.4641016151377544              [1, 0, 3] [3, 2, 1]
2.0                             [1, 0, 3] [1, 2, 3]
5.0990195135927845              [1, 0, 3] [4, 4, 4]
2.23606797749979                [1, 2, 3] [1, 1, 1]
2.8284271247461903              [1, 2, 3] [3, 2, 1]
2.0                             [1, 2, 3] [1, 0, 3]
3.7416573867739413              [1, 2, 3] [4, 4, 4]
5.196152422706632               [4, 4, 4] [1, 1, 1]
3.7416573867739413              [4, 4, 4] [3, 2, 1]
5.0990195135927845              [4, 4, 4] [1, 0, 3]
3.7416573867739413              [4, 4, 4] [1, 2, 3]

How do I sum up all the numbers that start with [1,1,1], [3,2,1]...etc and then compare the distances with each and then return the index with the lower sum?

1
  • Did you solve this? Was any of the answer useuful? Commented Dec 9, 2011 at 20:00

2 Answers 2

0

Already checked this or this or even this? There are many solutions on stackoverflow or other platforms, and I'm sure you checked them out. So what'ts wrong with them?

Sign up to request clarification or add additional context in comments.

2 Comments

lol i am sorry i tottaly forogot to check with euclidean distance as search parameter. as you can see i didnt even use it in a question
now i checked them and thats not what i am asking for. i know how to get distance my function euclid does it (tho i didnt publish the code for it) the problem is how do i get the list inside list that has the shortest euclidean distance, what condition should i use. i should be compering all the distances for each list and then chose list wit the shortest distances comparing to others. Also i dont have numpy module and as far as i know i should not need it
0

Can you just use two variables to keep track of the lowest?

lowest_vsota = 0
lowest_coord = []

Then in the if block ...

if vsota < lowest_vsota:
    lowest_vsota = vsota
    lowest_coord = j

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.