[转载]C# List<T>的并集、交集、差集

原文地址:http://www.cnblogs.com/godbell/p/7535637.html

集合的并集是合并集合的项,如下图所示:

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};

IEnumerable<int> unionLs = ls1.Union(ls2);
foreach (int item in unionLs)
{
    Console.Write("{0}\t", item);
}

集合的交集是取集合的共同的项,如下图所示:

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};

IEnumerable<int> intersectLs = ls1.Intersect(ls2);
foreach (int item in intersectLs)
{
    Console.Write("{0}\t",item);
}

集合的差集是取在该集合中而不在另一集合中的所有的项,如下图所示:

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};

IEnumerable<int> exceptLs = ls1.Except(ls2);
foreach (int item in exceptLs)
{
    Console.Write("{0}\t", item);
}

时间: 2024-07-28 20:23:30

[转载]C# List<T>的并集、交集、差集的相关文章

python两个 list 获取交集,并集,差集的方法

有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总结在下面:1. 获取两个list 的交集 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] #方法二 print list(set(a).intersection(set(b))) 2. 获取两个list 的并集 print list(set(a).union(set(b))) 3. 获取两个

Sql Server中集合的操作(并集、差集、交集)学习

首先我们做一下测试数据 1.创建测试数据 --创建人员表1-- create table Person1 ( Uid int identity(1,1) primary key, Name varchar(20) not null ) --创建人员表2-- create table Person2 ( Uid int identity(1,1) primary key, Name varchar(20) not null ) --插入数据-- insert into Person1 values

Linux 求文件交集 差集等

使用comm命令 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是grep命令.分别介绍如下: comm命令 , Compare sorted files FILE1 and FILE2 line by line. With  no options, produce three-column output.  Column one contains lines un

python list 交集 差集

有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总结在下面:1. 获取两个list 的交集 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] #方法二 print list(set(a).intersection(set(b))) 2. 获取两个list 的并集 print list(set(a).union(set(b))) 3. 获取两个

set,map,list集合的交集差集存放相同值反转分割等-代码优雅之道Guava(三)

前言 前边已经对Guava类库做了简单介绍(博文:Java代码优雅之道-Guava(有相关jar包下载)),下面就简单介绍操作Set,Map,List集合的工具类的使用,学会灵活使用这些工具,会使用我们在开发中,更加省时省力,代码健壮性和可读性更高,更简洁,这也是我所谓的"代码优雅",让我们开发中省出时间来去处理更重要的东西,废话不多说,一看代码就全明了了. 创建泛型集合更加简介 //创建泛型集合更加简介 List<String> stringList=Lists.newA

python的交集、并集、差集

①差集 方法一: if __name__ == '__main__': a_list = [{'a' : 1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : 5}] b_list = [{'a' : 1}, {'b' : 2}] ret_list = [] for item in a_list: if item not in b_list: ret_list.append(item) for item in b_list: if item not in a_l

java消除 list重复值及交集,并集,差集

消除 list重复值 Java代码 public void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); } 交集: Java代码 list.retainAll(Arrays.asList(arr)); // list 中的就是交集了  arr为数组 差集: Java代码 list1.removeAll(list2); 并集: Java代码 list1.addAl

SQL 操作结果集 -并集、差集、交集、结果集排序

操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或多个结果集进行连接,形成“并集”.子结果集所有的记录组合在一起形成新的结果集.  1.限定条件 要是用Union来连接结果集,有4个限定条件. (1).子结果集要具有相同的结构. (2).字结果集的列数必须相同. (3).子结果集对应的数据类型必须可以兼容. (4).每个子结果集不能包含order

交集、并集、差集

1 #include<stdio.h> 2 #include<set> 3 using namespace std; 4 const int N=1000+5; 5 6 int n,m; 7 int a[N], b[N]; 8 9 int main() 10 { 11 int i, j; 12 while(scanf("%d", &n)==1) 13 { 14 set<int> s; //保存并集 15 set<int> cha;