我一直試圖從這個列表中獲取mediana,這意味着列表的最短Euclidean distance。Python:列表中的列表
我做了一個函數euclid,它返回2個向量之間的距離,不管它們的大小如何,但我有2個for循環的問題。
這個程序應該返回[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)
但當前返回:
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]
如何總結所有以[1,1開頭的數字, 1],[3,2,1] ...等,然後比較每個距離,然後返回索引與較低的總和?
你解決這個問題?有沒有答案有用? – mac