简体   繁体   English

比较列表中的每个项目与另一个列表中的另一个项目

[英]compare each item in list with another item in another list

I have 2 list of double 我有2张双人床

List<double> numbers1 = [1,3,5,6,4.5,2.1,.......];
List<double> numbers2 = [.5,3.2,5.4,6,4.5,2.1,.......];

I want to compare [1] in list1 whith [.5] in list 2 我想比较列表1中的[1]和列表2中的[.5]

if([1] close to[.5]) for example if([1] close to[.5])

which means if the first item value in fisrst list close to the first item value in second list and etc, the second with second, third with third 这意味着,如果第一个列表中的第一个项目值接近第二个列表中的第一个项目值,依此类推,则第二个与第二个,第三个与第三个

How can I do this c#? 我该怎么做C#?

Simply check for the difference between two related items using Math.Abs : 只需使用Math.Abs检查两个相关项目之间的差异:

bool allElementsClose = true;
for(int i = 0; i < lis1.Count && allElementsClose; i++)
{
    if(Math.Abs(list1[i] - list2[i]) > threshold) 
    {
        Console.WiteLine("items not close");
        allElementsClose = false;
        break;
    }
}

You should however make some checks on the number of items before iterating. 但是,您应该在迭代之前对项目数量进行一些检查。

Alternative approach using LINQ: 使用LINQ的替代方法:

var allElementsClose = list1
    .Select((x, i) => Math.Abs(x - list2[i]))
    .All(x => x < threashhold);

This approach uses the overload of select that uses a Func<T, int> for getting the element and its index within the collection. 此方法使用select的重载,该重载使用Func<T, int>来获取元素及其在集合中的索引。 This index can then be used to get the appropriate item in the second list and calculate the difference to it. 然后可以使用该索引来获取第二个列表中的适当项目,并计算与之的差。

Simplest solution would be using Zip , which is meant for comparison of each mapping element of two collections and will also take care of difference in number of elements, as it checks the Enumerator.MoveNext() internally 最简单的解决方案是使用Zip ,它用于比较两个集合的每个映射元素,并且还将在内部检查Enumerator.MoveNext()元素数量的差异。

var result = numbers1.Zip(numbers2,(n1,n2) => n1 - n2 < 0.5 ? "Close" : "Not Close");

result would IEnumerable<string> consisting of values "Close" and "Not Close" result将是IEnumerable<string>由值"Close" and "Not Close"

Caveat: 警告:

  • Your question has good number of details missing, as I am not sure what is "Close" and what is "Not Close", I have assumed less than 0.5 difference between two numbers is close, modify as per requirement 您的问题缺少很多详细信息,因为我不确定什么是“关闭”和什么是“未关闭”,我假设两个数字之间的差值小于0.5,请根据要求进行修改

Actually it depends on what "close" means for you. 实际上,这取决于“关闭”对您的意义。 I will assume that if the difference between the numbers is less than 1, it is close. 我将假设,如果数字之间的差小于1,则接近。

So the code will be really simple 因此,代码将非常简单

I will create 3 lists. 我将创建3个列表。 list1, list2, listResult (which will only contain Boolean values, that indicates whether the item in list1 is close to the item in list2. list1,list2,listResult (将仅包含布尔值,该布尔值指示list1中的项目是否接近list2中的项目。

var list1 = new List<float>();
var list2 = new List<float>();
var listResult = new List<bool>();

The 3 lines above, should go outside the method or event, like button1_click. 上面的3行应超出方法或事件的范围,例如button1_click。

//to add values to the list you can use list1.addNew(5) and it will add the "5" to the list

void CompareLists(){
    //first of all we check if list1 and list2 have the same number of items
    if(list1.Count == list2.Count){
        for (int i=0; i<list1.Count; i++){                
            if (list1[i] - list2[i] < 1)
               listResult.addNew(true);
            else
               listResult.addNew(false);
        }
    }
}

Now you have a list of boolens like [true],[true],[false] 现在,您有一个布尔值列表,例如[true],[true],[false]

Hope, it works for you. 希望这对你有用。

In .Net 4.0 you also have Zip() available: 在.Net 4.0中,您还可以使用Zip()

var numbers1 = new List<double>() { 1, 3, 5, 6, 4.5, 2.1 };
var numbers2 = new List<double>() { 0.5, 3.2, 5.4, 6, 4.5, 2.1 };

var allElementsClose = numbers1
    .Zip(numbers2, (i1, i2) => Math.Abs(i1 - i2))
    .All(x => x < threshold);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将每个项目创建为另一个列表? - how to create list each item as another list? 将每个列表项复制到另一个列表项属性 - Copy each list item to another list item attribut 比较一个列表对象中的项目与另一个列表对象中的另一项目 - Compare the item from one list object with another item from another list object Viewmodel有一个项目列表,每个项目都有另一个项目列表 - Viewmodel has a list of items and each item has another list of items 列表给另一个列表,列表中的每个项目给另一个列表,需要在excel中记录 - list give another list, each item in list give another list, need to record in excel c#查找LINQ查询返回的列表中的项目,并将其值与列表中的另一项进行比较 - c# find item in list returned by LINQ query and compare its value with another item in list 项目命令无法编辑列表中的另一个项目 - Item command cannot edit another item in the list 分割字符串并使用每个项目分配给另一个列表 - split a string and use each item to assign to another list 如何在另一个列表中创建每个项目的列表,以及该项目出现的次数? - How can I create a list of each item in another list, and the count of how many times that item appears? Linq位置列表包含另一个列表中的项目 - Linq Where list Contains item in another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM