-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?

Edwin
  • 2,074
  • 1
  • 21
  • 40
buco
  • 39
  • 6

2 Answers2

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
jcfollower
  • 3,103
  • 19
  • 25
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?

Community
  • 1
  • 1
gecco
  • 17,969
  • 11
  • 51
  • 68
  • 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 – buco Dec 04 '11 at 16:26
  • 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 – buco Dec 04 '11 at 16:43