我有两个像这样的数组,我想找到匹配和不匹配的Point3d值。
a = [['xxx', [0.203125, 0.203125, 0.0],
Point3d(12, 1.25984, -0.275591), nil],
['eee', [0.203125, 0.203125, 0.0],
Point3d(12, 20.0995, -0.275591), nil]]
b = [['aaa', [0.203125, 0.203125, 0.0],
Point3d(10, 1.25984, -0.275591), nil],
['sss', [0.203125, 0.203125, 0.0],
Point3d(10, 20.0995, -0.275591), nil],
['www', [0.203125, 0.203125, 0.0],
Point3d(12, 1.25984, -0.275591), nil],
['nnn', [0.203125, 0.203125, 0.0],
Point3d(12, 20.0995, -0.275591), nil]]
应该返回2个数组,其中一个具有匹配的Point3ds。。。
result_match = [['xxx', [0.203125, 0.203125, 0.0],
Point3d(12, 1.25984, -0.275591), nil],
['eee', [0.203125, 0.203125, 0.0],
Point3d(12, 20.0995, -0.275591), nil]]
和不匹配的Point3ds。。。
result_non_match = [['aaa', [0.203125, 0.203125, 0.0],
Point3d(10, 1.25984, -0.275591), nil],
['sss', [0.203125, 0.203125, 0.0],
Point3d(10, 20.0995, -0.275591), nil]]
我已经搜索并尝试了显示的结果,但它们似乎不起作用,因为它们都适用于数组,而不是point3ds。我发现的最接近的是这个,但我无法让它发挥作用。。。
samepoint = a.map(&:to_a) & b.map(&:to_a)
您想要创建点集。
a_set = a.map { |x| x[2] }.to_set
b_set = b.map { |x| x[2] }.to_set
现在,您可以使用集合交集&
查找两个集合中出现的点,并使用^
查找差异。
intersect = a_set & b_set
diff = a_set ^ b_set
得到这些值后,只需要过滤数组以匹配这些值。
matches = (a + b).select { |x| intersect.include?(x[2]) }