我试图写一个函数,在这里我可以吐出同一行中的所有点。我计算的事实是,两对点之间的斜率必须是相同的。
我已经迭代了输入文件,得到一个点的列表和计算的斜率。我的下一步将是将它们放到一个HashMap上(或者在Python中),关键是斜率,并使用点和斜率更新它。如果这两个数字的斜率已经存在,请将点添加到相同的条目中,并删除任何重复项。
我能够提取输入,计算斜率,并将它们放在一个hashmap中。但是,将它们放到hashmap上对我来说有点困难,因为我正在尝试使用我熟悉的类似Java的语法。
有人能帮我更新hashmap确保没有被插入吗?
以下是我迄今所做的工作:
slopeMap = {}
for x in range (0, len(arr)):
for y in range (x+1, len(arr)):
slopeForPoints = (slope(arr[x][0], arr[y][0], arr[x][1], arr[y][1]))
if slopeMap.has_key(slopeForPoints) == False:
slopeMap[slopeForPoints].append()
"slopeForPoints" in slopeMap
slopeMap["slopeForPoints"] =
a.setdefault("somekey",[]).append("bob")
print slopeForPoints我只需要在上面的功能上的帮助。斜率和迭代函数,我得到了工作。
样本斜率值(Key- HashMap)
0.0
1.0
0.0
0.9样本点值(值- HashMap)
0.0,0.0
1.1,1.1
3.5,4.5
2.2,2.2发布于 2018-02-25 06:06:44
正如疯狂物理学家所提到的,你需要计算的不仅仅是斜率来识别唯一的直线,因为平行线有相同的斜率,但不一定是同一条线。
这方面有几个选择。其中一个选项是使您的字典元组(如(slope, intercept) )的键。然后,为了确保这些点是唯一的,您可以为您的元组字典集创建值。
这个想法应该是这样的:
slope, intercept = slope_intercept(point1, point2) #Each point is (point_x, point_y)
#Need to write the slope_intercept function
if (slope, intercept) not in slopeMap:
slopeMap[(slope,intercept)] = set() #Could be done with a defaultDict instead
slopeMap[(slope,intercept)].add(point1))
slopeMap[(slope,intercept)].add(point2))注意,更多的是Pythonic说
if slopeForPoints not in slopeMap:https://stackoverflow.com/questions/48970554
复制相似问题