[英]Sketchup: Ruby array return matching and non matching sub arrays with specific element
I have 2 arrays that look like this and I'd like to find the matching and non matching Point3d values.我有 2 个 arrays 看起来像这样,我想找到匹配和不匹配的 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]]
should return 2 arrays, one with matching Point3ds...应该返回 2 arrays,一个与匹配的 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]]
and non matching Point3ds...和不匹配的 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]]
I've searched and tried the results that show up but they don't seem to work since they all work with arrays and not point3ds.我搜索并尝试了显示的结果,但它们似乎不起作用,因为它们都适用于 arrays 而不是 point3ds。 The closest I found is this but I can't get that to work...
我找到的最接近的是这个,但我无法让它工作......
samepoint = a.map(&:to_a) & b.map(&:to_a)
You want to create sets of points.您想要创建点集。
a_set = a.map { |x| x[2] }.to_set
b_set = b.map { |x| x[2] }.to_set
Now you can find points which occur in both sets using a set intersection &
, and the difference using ^
.现在,您可以使用集合交集
&
查找出现在两个集合中的点,使用^
查找差异。
intersect = a_set & b_set
diff = a_set ^ b_set
Having gotten these values, need only filter your arrays to match for those values.获得这些值后,只需过滤您的 arrays 以匹配这些值。
matches = (a + b).select { |x| intersect.include?(x[2]) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.