首页 > 编程语言 >C# 中实现对List中的数据查重操作

C# 中实现对List中的数据查重操作

时间:2023-05-31 18:02:14浏览次数:38  
标签:查重 Count Console C# List 列表 WriteLine new

一、列表查重操作核心如下
1.1、常用列表的查重操作核心如下:

//查找出列表中的所有重复元素
      private static List<string> QueryRepeatElementOfList(List<string> list)
      {
          List<string> listTmp = new List<string>();
          if (list!=null && list.Count>0)
          {
              listTmp = list.GroupBy(x => x)
                            .Where(g => g.Count() > 1)
                            .Select(y => y.Key)
                            .ToList();
          }
          return listTmp;
      }

  
      //查找出列表中的所有重复元素及其重复次数
      private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
      {
          Dictionary<string,int> DicTmp = new Dictionary<string, int>();
          if (list != null && list.Count > 0)
          {
              DicTmp = list.GroupBy(x => x)
                           .Where(g => g.Count() > 1)
                           .ToDictionary(x => x.Key, y => y.Count());
          }
          return DicTmp;
      }

1.2、类列表的查重数据操作核心如下

//静态扩展类(实现对类重复内容的操作)
   public static class Extention
   {
       public static IEnumerable<T> GetMoreThanOnceRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
       { //返回第二个以后面的重复的元素集合
           return extList
               .GroupBy(groupProps)
               .SelectMany(z => z.Skip(1)); //跳过第一个重复的元素
       }
       public static IEnumerable<T> GetAllRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
       {
           //返回所有重复的元素集合
           return extList
               .GroupBy(groupProps)
               .Where(z => z.Count() > 1) 
               .SelectMany(z => z);
       }
   }

二、使用方法

 
 //货物信息
    class GoodsInfo
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
 
        public GoodsInfo()
        {
                
        }
 
        public GoodsInfo(string code,string name,string position)
        {
            this.Code = code;
            this.Name = name;
            this.Position = position;
        }
    }
 
  class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("------------------列表操作-------------------------");
            //初始化列表
            List<string> listTmp = InitList();
 
            //查找出列表中的所有重复元素
            List<string> repeatList = QueryRepeatElementOfList(listTmp);
            if (repeatList!=null && repeatList.Count>0)
            {
                Console.WriteLine("---当前所有的重复元素为:---");
                foreach (var item in repeatList)
                {
                    Console.WriteLine(item);
                }
            }
 
            //查找出列表中的所有重复元素
            Dictionary<string, int> repeatEleAndCountDic = QueryRepeatElementAndCountOfList(listTmp);
            if (repeatEleAndCountDic != null && repeatEleAndCountDic.Count > 0)
            {
                Console.WriteLine("---当前所有的重复元素个数为:---");
                foreach (var item in repeatEleAndCountDic)
                {
                    Console.WriteLine("重复内容为:"+item.Key+ " 重复次数为:"+item.Value);
                }
            }
 
            List<int> IntTmp = new List<int> { 10, 20, 30, 20, 50,10 };
            int Sum = IntTmp.Sum();
            Console.WriteLine("---获取列表的和:{0}",Sum);
            double Average = IntTmp.Average();
            Console.WriteLine("---获取列表的平均数:{0}", Average);
            int Max = IntTmp.Max();
            Console.WriteLine("---获取列表的最大值:{0}", Max);
            int Min = IntTmp.Min();
            Console.WriteLine("---获取列表的最小值:{0}", Min);
            List<int> DistinctInfo = IntTmp.Distinct().ToList();
            foreach (var item in DistinctInfo)
            {
                Console.WriteLine("---获取列表的不重复数据:{0}", item);
            }
           
 
            Console.WriteLine();
            Console.WriteLine("-------------------类列表操作---------------------------");
            //初始化类列表
            List<GoodsInfo> goodsInfos = InitGoodsInfoList();
 
            //查询到列表中的所有重复数据
            List<GoodsInfo> AllRepeatGoodsInfos = QueryAllRepeatInfoOfList(goodsInfos);
            if (AllRepeatGoodsInfos!=null && AllRepeatGoodsInfos.Count>0)
            {
                Console.WriteLine("---当前【满足(名称与位置相同)条件】所有的重复的货物信息为:---");
                foreach (var item in AllRepeatGoodsInfos)
                {
                    Console.WriteLine("编号:{0},名称:{1},位置:{2}",item.Code,item.Name,item.Position);
                }
            }
 
            //查询到列表中重复一次以上的数据
            List<GoodsInfo> RepeatMoreThanOnceGoodsInfos = QueryMoreThanOnceInfoList(goodsInfos);
            if (RepeatMoreThanOnceGoodsInfos != null && RepeatMoreThanOnceGoodsInfos.Count > 0)
            {
                Console.WriteLine("---当前【满足(名称相同)条件】所有的重复一次以上的货物信息为:---");
                foreach (var item in RepeatMoreThanOnceGoodsInfos)
                {
                    Console.WriteLine("编号:{0},名称:{1},位置:{2}", item.Code, item.Name, item.Position);
                }
            }
 
            Console.ReadLine();
        }
 
 
        #region   列表操作
 
        //初始化一个列表
        private static List<string> InitList()
        {
            List<string> listTmp = new List<string>();
            string str = "101";
            for (int i = 0; i < 3000; i++)
            {
                string strTmp = str + i;
                switch (strTmp)
                {
                    case "10112":
                    case "10118":
                    case "10124":
                        strTmp = "10107";
                        break;
                    case "10126":
                    case "10137":
                    case "10138":
                    case "10149":
                        strTmp = "10111";
                        break;
                    default:
                        break;
                }
                listTmp.Add(strTmp);
 
            }
 
            return listTmp;
        }
 
 
 
        //查找出列表中的所有重复元素
        private static List<string> QueryRepeatElementOfList(List<string> list)
        {
            List<string> listTmp = new List<string>();
            if (list!=null && list.Count>0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }
 
    
        //查找出列表中的所有重复元素及其重复次数
        private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
        {
            Dictionary<string,int> DicTmp = new Dictionary<string, int>();
            if (list != null && list.Count > 0)
            {
                DicTmp = list.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .ToDictionary(x => x.Key, y => y.Count());
            }
            return DicTmp;
        }
 
        #endregion
 
 
        #region   类操作
 
        //初始化类列表
        private static List<GoodsInfo> InitGoodsInfoList()
        {
            List<GoodsInfo> goodsInfos = new List<GoodsInfo>()
            {
                new GoodsInfo("10101","海尔冰箱","0102"),
                new GoodsInfo("10102","海尔电视","0103"),
                new GoodsInfo("10103","海尔空调","0104"),
                new GoodsInfo("10104","海尔净化器","0105"),
                new GoodsInfo("10105","海尔冷风机","0106"),
 
            };
 
            GoodsInfo goodsInfo1 = new GoodsInfo("10106", "海尔冰箱", "0102");
            GoodsInfo goodsInfo2 = new GoodsInfo();
            goodsInfo2.Code = "10108";
            goodsInfo2.Name = "海尔净化器";
            goodsInfo2.Position = "0108";
 
            goodsInfos.Add(goodsInfo1);
            goodsInfos.Add(goodsInfo2);
 
            return goodsInfos;
 
        }
 
        //查询到列表中的所有重复数据(条件为:名称与位置相同)
        private static List<GoodsInfo> QueryAllRepeatInfoOfList(List<GoodsInfo> goodsInfos)
        {
            List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();
 
            if (goodsInfos!=null && goodsInfos.Count>0)
            {
                goodsInfoTmpList=goodsInfos.GetAllRepeatInfo(x => new { x.Name, x.Position })
                                           .ToList();
                         
            }
            return goodsInfoTmpList;
        }
 
        //查询到重复了一次以上的数据
        private static List<GoodsInfo> QueryMoreThanOnceInfoList(List<GoodsInfo> goodsInfos)
        {
            List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();
 
            if (goodsInfos != null && goodsInfos.Count > 0)
            {
                goodsInfoTmpList = goodsInfos.GetMoreThanOnceRepeatInfo(x => new { x.Name})
                                           .ToList();
 
            }
            return goodsInfoTmpList;
        }
 
        #endregion
 
 
    }//Class_end

三、结果如下:

参考:https://blog.csdn.net/xiaochenXIHUA/article/details/107020130

标签:查重,Count,Console,C#,List,列表,WriteLine,new
From: https://www.cnblogs.com/nuomibaibai/p/17446921.html

相关文章

  • macos安装nvm管理多版本node
    最早直接采用brew安装,如下:brewinstallnode@18brewuninstallnode@18 //卸载 但学习的项目用的是老版本node,所以卸载了,用NVM来管理多版本node,参考这篇文章:https://blog.bigoodyssey.com/how-to-manage-multiple-node-versions-in-macos-2021-guide-5065f32cb63b同时加......
  • Pytest - Fixture(12) - 配置文件conftest.py
    Pytest-配置文件-conftest.py前言如果在多个测试文件中的用到相同的fixture函数,则可以将其移动到conftest.py文件中conftest.py是专门存放fixture的配置文件;例如:如果测试用例都需要进行用户登录的时候,仅需将登录的功能放到conftest.py文件中,而不需要在每个用......
  • Linux网络性能评估工具iperf 、CHARIOT测试网络吞吐量
    网络性能评估主要是监测网络带宽的使用率,将网络带宽利用最大化是保证网络性能的基础,但是由于网络设计不合理、网络存在安全漏洞等原因,都会导致网络带宽利用率不高。要找到网络带宽利用率不高的原因,就需要对网络传输进行监控,此时就需要用到一些网络性能评估工具,而Iperf就是这样一款......
  • c++算法:二分
    算法中,有一种比线性查找算力费得更少的一种算法思想,叫“分治”,今天讲的是分治里的二分查找:借助(low+high)/2公式,找到搜索区域内的中间元素。图1中,搜索区域内中间元素的位置是 ⌊(1+10)/2⌋=5,因此中间元素是27,此元素显然不是要找的目标元素。然后就是缩小范围。 下面就是......
  • spring cloud 之 openfeign 记录(通过feign上传)
    今日搭建好nacos nacos踩坑记录迫不及待的进入了下一步,服务间的远程调用,就踩了一个小小的坑 我做的是一个阿里oss上传的服务!阿里oss服务个人可以有三个月试用,对新手非常的友好首先是一个openfeign编写上的问题@RequestMapping(value="/common/oss/download",meth......
  • Cookie、Session、Token、LocalStorage、SessionStorage
    Cookie简介:HTTP是无状态的,服务器无法记录收到的每一次请求,意味着服务器无法识别不同的请求是否来自相同的客户端。Cookie是服务器创建的一个对象,在收到客户端请求后,携带在响应头(Set-Cookie)中返回给客户端,客户端将Cookie存到本地,在下一次请求中将Cookie信息放到请求头发......
  • 基于multiprocessing map实现python并行化(全局变量共享 map机制实用向分析 常见问题 p
    转载:(15条消息)基于multiprocessingmap实现python并行化(全局变量共享map机制实用向分析常见问题pandas存储数据)_goto_past的博客-CSDN博客基于multiprocessingmap实现python并行化之前从来没考虑python可以并行化,最近有一个项目需要计算100*100次的遗传算法适应度,每次计算......
  • CODE FESTIVAL 2016 qual C
    你说的对,但是我觉得应该先把qualC写完再去学什么东西。CFcodeforces.#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<iostream>usingnamespacestd;intn;chars[110];intmain(){scanf("%s",s+1)......
  • C++ 初始化赋值
    把值写在小括号中,等于号可以省略(C++标准)inta=(15);intb(20);把值写在花括号中,等于号也可以省略(C++11标准),统一初始化列表注意:在Linux平台下,编译需要加-std=c++11参数inta={15};inta{15};......
  • JDK高版本反射修改 private static fianl 修饰的对象
    在JDK高版本中,Java语言规范已经更新,因可能会破坏Java语言的安全性和稳定性,不再允许通过反射改变final字段的值,需要自己做一下处理。 创建工具类importjava.lang.reflect.Field;importsun.misc.Unsafe;publicclassFieldUtil{privatestaticUnsafeunsafe......