我有两套,每套都包含以下类别的对象:
class pointInfo:
x = 0
y = 0
steps = 0
我想找到两个集合的交集,但只在x和y值上。 比如:
def findIntersections(pointInfoSet1, pointInfoSetwire2):
return [pointInfo for pointInfo in pointInfoSet1 if pointInfo.x, pointInfo.y in pointInfoSet2]
我知道,如果在python中有两个集合,那么只需执行set1.intersection(set2),但在本例中这不起作用,因为我只想找到对象属性的某个子集在哪里是相同的,而不是相同的对象。提前谢谢
下面是一个解决方案,它使
Point
对象在其x和y属性上都是Hashable
:然后我们可以将它们放在一组中,自然地获得交点:
假设没有导线自身交叉,更好的解决方案可能是通过两条导线进行迭代,保持点到步长的dict,并输出每个交点处的步长总和:
相关问题 更多 >
编程相关推荐