2016-02-12 86 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))