2016-02-12 131 views
-2

這是我的init類:曼哈頓距離的Python

高清的init(自我,行,列): self.row =行 self.column =列

高清manhattan_distance(開始,結束): 「」「返回曼哈頓距離b/w的開始和結束。 ‘’」

coordinate_dict = {} 
    for row in enumerate(start): 
     for column in enumerate(start): 
      coordinate_dict[value] = (row, column) #row and column are not being added in coordinate_dict under value and how would i go after 

我需要計算從開始到結束的距離。根據這個公式= |結束(行) - 開始(行)| + |結束(列) - 開始(列)|

注意:我的行和列在init類。

+2

什麼是你的問題? – 2016-02-12 13:29:30

+0

曼哈頓距離定義爲座標絕對差值的總和。 –

+0

如果我們不知道你在做什麼,我們不能告訴你如何繼續。 – zondo

回答

7

假設startend(x, y)對,

def manhattan_distance(start, end): 
    sx, sy = start 
    ex, ey = end 
    return abs(ex - sx) + abs(ey - sy) 

對於更高的維度,這可以延伸,就像

def manhattan_distance(start, end): 
    return sum(abs(e - s) for s,e in zip(start, end))