简体   繁体   English

比较数组并返回相似图C#

[英]Comparing arrays and returning a similarity figure C#

I am trying to return a number that represents the similarity between two arrays. 我试图返回一个数字,代表两个数组之间的相似性。
Ie : 即:

Array1: {Katy, Jenny, Sarah, Ben, Jill, Tina} 
Array2: {Katy, John, Sam, Ben, Jill, Linda}

I want to return the number 3 because three comparisons are correct. 我想返回数字3,因为三个比较正确。 Is this possible? 这可能吗? I can't think of any functions that will do this for me. 我想不出任何可以为我做的功能。

This is how you can count the amount of items that are equal in matching indices. 这是您如何计算匹配索引中相等项目的数量。

var c = arr1.Where((x, i) => x.Equals(arr2[i])).Count();

Note that you might want to assure that you don't try to access arr2 in an index that is out of range: 请注意,您可能要确保不要尝试在超出范围的索引中访问arr2:

var c = arr1.Take(arr2.Length).Count(...);

If you don't care about index positions, you should use nemesv's solution. 如果您不在乎索引位置,则应使用nemesv的解决方案。

There are many ways to do this. 有很多方法可以做到这一点。 Since others have already specified a few ways, I will try to post a different way of doing the same. 由于其他人已经指定了几种方法,因此我将尝试发布一种不同的方法。

If you consider matching based on index, you can do something like this using Zip 如果您考虑根据索引进行匹配,则可以使用Zip做类似的事情

var cnt = 0;
Array1.Zip(Array2,(a,b)=>{
    if(a.Equals(b)) ++cnt; 
    return string.Empty; //we dont need this
}).Count(); // use tolist or count to force evaluation

If you don't care about ordering and are just concerned about matching, you can use Intersect 如果您不关心订购而只关心匹配,则可以使用Intersect

Array1.Intersect(Array2).Count()

The way I would approach this problem is too take the value in the first array and compare it with every other value in the second array. 我要解决此问题的方法也是将第一个数组中的值与第二个数组中的每个其他值进行比较。 If they match than increase a compare counter and that will tell you their are three comparisons that match. 如果它们匹配,则增加一个比较计数器,这将告诉您它们是三个匹配的比较。

This works for me: 这对我有用:

var array1 = new string[] {"Katy", "Jenny", "Sarah", "Ben", "Jill", "Tina"};
var array2 = new string[] {"Katy", "John", "Sam", "Ben", "Jill", "Linda"};

var similarity = (array1.Length + array2.Length) - array1.Union(array2).Count();

Edit: Oh just saw you want them to be in the same position. 编辑:哦,刚刚看到您希望它们位于相同的位置。

You're saying "According to index", assuming you mean that if "John" is on position 1 in the first list, and on position 2 on the second list => no match. 您说的是“根据索引”,假设您的意思是如果“约翰”在第一个列表中的位置1,在第二个列表中的位置2 =>没有匹配。

In that case: 在这种情况下:

int maxItems = Math.Min(arr1.Length, arr2.Length);
int matchCount = 0;

for(int i = 0; i < maxItems; i++)
{
  if(object.Equals(arr1[i], arr2[i]))
    matchCount++;
}

I'd do it like this: 我会这样:

int count = array1.Zip(array2, (a, b) => a.Equals(b)).Count(b => b);

The zip part returns an IEnumerable<bool> and the count part count how many times true occurs in that list. zip部分返回IEnumerable<bool> ,而count部分计算该列表中true发生的次数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM