英文:
Check if elements in a list<t> exist in the beginning or in the end of another list in C#
问题
List1:
List<Segment> segments = new();
List2:
List<Point> points = new();
List 1 包括 List 2 的多个/所有元素(已排序的元素),有时 List1 不包括 List2 的一些元素,无论是从 List1 的开头还是结尾。
示例图像如下,如何最容易地获取 List2 中的这些多个第一个/最后一个不存在的元素。
英文:
I have two lists List1 and List2
List1:
List<Segment> segments = new();
List2:
List<Point> points = new();
List 1 includes multiple/all elements of list 2(sorted elements), sometimes list1 will not include few elements of list2 either from the beginning or the end of the list1.
For illustration:
what is the easiest way to get these multiple first/last non-existing elements of list2.
答案1
得分: 0
如果我理解正确你的问题,你需要:
- 将你的第一个分段列表扁平化为一个点列表。通常可以使用Linq运算符
SelectMany
来实现这一点。 - 将你的扁平化的点列表与第二个点列表相交,并识别那些不在任何分段中的点。通常可以使用Linq运算符
Except
来实现这一点。
你可以按照以下方式进行:
var pointsInSegments = segments.SelectMany(s => s.Points);
var pointsNotInSegments = points.Except(pointsInSegments);
英文:
If I'm understanding correctly your problem, you need to:
- Flatten your first list of segments into a list of points. You typically do this with Linq operator
SelectMany
. - Intersect your flattened point list with your second list of points and identify those that are not in any segment. You typically do this with Linq operator
Except
.
You'd do this the following way:
var pointsInSegments= segments.SelectMany(s => s.Points);
var pointsNotInSegments = points.Except(pointsInSegments);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论