我有两个列表(可能是也可能不是相同的长度)。在每个列表中,有一系列两点的元组(基本上是X,Y值)。比较两个点元组列表的更快方法?
我比较两个列表对彼此找到两个具有相似点值的点。我尝试了列表理解技术,但它真的让列表中的嵌套元组感到困惑,并且我无法让它工作。
这是做这个最好的(最快的)方法吗?我觉得可能会有更多的Pythonic这样做。
说我有两个列表:
pointPairA = [(2,1), (4,8)]
pointPairB = [(3,2), (10,2), (4,2)]
然后空列表,用于存储对和解包的元组的公差值仅存储配对
matchedPairs = []
tolerance = 2
然后这个循环,比较差异,并将它们添加到matchedPairs列表以指示匹配。
for pointPairA in pointPairListA:
for pointPairB in pointPairListB:
## Assign the current X,Y values for each pair
pointPairA_x, pointPairA_y = pointPairA
pointPairB_x, pointPairB_x = pointPairB
## Get the difference of each set of points
xDiff = abs(pointPairA_x - pointPairB_x)
yDiff = abs(pointPairA1_y - pointPairB_y)
if xDiff < tolerance and yDiff < tolerance:
matchedPairs.append((pointPairA, pointPairB))
这将导致matchedPairs这样看,里面都指向元组的元组:
[((2,1), (3,2)), ((2,1), (4,2))]
的列表中一个如果你可以用“距离”,而不是为容忍广场,你可以使用复杂的数字,而不是元组例如。 '[2 + 1j,4 + 8j]'。然后你可以比较'abs(pt1-pt2)'和容差 – 2011-06-08 02:01:06