gpt4 book ai didi

c# - 使用两个通用列表生成交集和分解列表

转载 作者:行者123 更新时间:2023-11-30 19:47:29 24 4
gpt4 key购买 nike

假设我们有两个 List<int>

List<int> list1 = new List<int> { 1, 3, 5, 7 , 9, 11, 18 };
List<int> list2 = new List<int> { 2, 3, 5, 7 , 9, 10, 20, 26, 36 };

问我怎么生产;

intersect  {3, 5, 7, 9 }
list1Decomp { 1, 11, 18 }
list2Decomp { 2, 10, 20, 26, 36 }

提前致谢。

最佳答案

var intersection = list1.Intersect(list2).ToList();
var list1Decomp = list1.Except(intersection).ToList();
var list2Decomp = list2.Except(intersection).ToList();

关于c# - 使用两个通用列表生成交集和分解列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com