[英]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: 警告:
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.