寻找一个C#算法来比较两个列表 [英] Looking for a C# algorithm to compare two lists

查看:63
本文介绍了寻找一个C#算法来比较两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试,但没有找到一个完全符合我需要的解决方案。所以我转向这里的小组寻求帮助。



我有两个List< string>我要比较的对象,我想找到两个列表中不同的项目。这两个列表已按字母顺序排序。



所以我需要一种方法来遍历列表以找到两个列表中都没有出现的内容。



例如...



列表1 = Apple,Banana,Carrot,葡萄,李子
清单2 =苹果,胡萝卜,葡萄,芒果,李子





我想要的是能够在列表1中找到 Banana ,在列表2中找到 Mango - 以我知道哪个列表的方式来自。



谢谢!

解决方案

使用Linq!



 List< string> List1 =  new 列表< string>(){  Apple   Banana 胡萝卜 葡萄  Plum}; 
List< string> List2 = new 列表< string>(){ Apple 胡萝卜 Grape 芒果 Plum};

var result = List1.Concat(List2) // 将列表连接到单个记录集
.Except(List1.Intersect(List2)) // 消除常见项
。选择(a => new
{
Value = a, // 获取价值
List = List1.Any(c => c == a)? List1 List2 // define list
});

foreach var d in result)
{
Console.WriteLine( 项目:'{0 ''发现于:'{1}',d.Value,d.List);
}





退货:

项目:'香蕉'发现于:'List1'
项目:'芒果'发现于:'List2'





试试!


 List< string> lstIn1NotIn2 = lst1.Except(lst2).ToList(); 
List< string> lstIn2NotIn1 = lst2.Except(lst1).ToList();


使用LINQ比较两个字符串列表 [ ^ ]

 class CompareLists 
{
static void Main()
{
//创建IEnumerable数据源。
string [] names1 = System.IO.File.ReadAllLines(@../../../ names1.txt);
string [] names2 = System.IO.File.ReadAllLines(@../../../ names2.txt);

//创建查询。请注意,此处必须使用方法语法。
IEnumerable< string> differenceQuery =
names1.Except(names2);

//执行查询。
Console.WriteLine(以下行在names1.txt中但不在names2.txt中);
foreach(stringsQuery in differenceQuery)
Console.WriteLine(s);

//在调试模式下保持控制台窗口打开。
Console.WriteLine(按任意键退出);
Console.ReadKey();
}
}
/ *输出:
以下行是names1.txt而不是names2.txt
Potra,Cristina
Noriega,Fabricio
Aw,Kam Foo
Toyoshima,Tim
Guy,Wey Yuan
Garcia,Debra
* /





-KR


I have been experimenting but haven't found a solution that does exactly what I need it to do. So I am turning to the group here to ask for help.

I have two List<string> objects that I want to compare, and I want to find any item that is not the same in both lists. Both lists are already sorted into alphabetical order.

So I need a way to iterate through the lists to find anything that does not appear in both lists.

For example...

List 1 = Apple, Banana, Carrot, Grape, Plum
List 2 = Apple, Carrot, Grape, Mango, Plum



What I want is to be able to find "Banana" in List 1 and "Mango" in List 2 - and in a way that I know which list it came from.

Thanks!

解决方案

Use Linq!

List<string> List1 = new List<string>(){"Apple", "Banana", "Carrot", "Grape", "Plum"};
List<string> List2 = new List<string>(){"Apple", "Carrot", "Grape", "Mango", "Plum"};

var result = List1.Concat(List2) //concatenate list into single recordset
		.Except(List1.Intersect(List2)) //eliminate common items
		.Select(a=>new
			{
				Value = a, //get value
				List = List1.Any(c=>c==a) ? "List1" : "List2" //define list
			});
			
foreach(var d in result)
{
	Console.WriteLine("Item: '{0}' found on: '{1}'", d.Value, d.List);
}



Returns:

Item: 'Banana' found on: 'List1'
Item: 'Mango' found on: 'List2'



Try!


List<string> lstIn1NotIn2 = lst1.Except(lst2).ToList();
List<string> lstIn2NotIn1 = lst2.Except(lst1).ToList();


Use LINQ to compare two lists of strings[^]

class CompareLists
{        
    static void Main()
    {
        // Create the IEnumerable data sources.
        string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt");
        string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt");

        // Create the query. Note that method syntax must be used here.
        IEnumerable<string> differenceQuery =
          names1.Except(names2);

        // Execute the query.
        Console.WriteLine("The following lines are in names1.txt but not names2.txt");
        foreach (string s in differenceQuery)
            Console.WriteLine(s);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}
/* Output:
     The following lines are in names1.txt but not names2.txt
    Potra, Cristina
    Noriega, Fabricio
    Aw, Kam Foo
    Toyoshima, Tim
    Guy, Wey Yuan
    Garcia, Debra
     */



-KR


这篇关于寻找一个C#算法来比较两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆