如何比较一个字典的值? [英] How to compare values of one dictionary?

查看:106
本文介绍了如何比较一个字典的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字典,每个键可以使用多个值(即Dictionary< string,Hashset< string>>)和我已经填写了我的字典,如下所示:

键值

电影0,电影1,电影2,电影7
B电影0,电影2,电影7

我有一个计数器,应计算这两个键之间的每个匹配(即键A,键B),所以在这种情况下,计数器应返回3,是否有循环每个键的值的方法,每次找到匹配时,它会递增计数器?

解决方案

听起来很简单:

  public   static   int  CountCommonValues< TKey,TValue>(
IDictionary< TKey,HashSet< ; TValue>>字典,
TK ey keyA,TKey keyB,IEqualityComparer< TValue> comparer = null
{
if (dictionary == null throw new ArgumentNullException( dictionary);

HashSet< TValue> valuesA,valuesB;
if (!dictionary.TryGetValue(keyA, out valuesA)) return 0 ;
if (!dictionary.TryGetValue(keyB, out valuesB)) return 0 ;
if (valuesA == null || valuesB == null return 0 ;

return valuesA.Intersect(valuesB,comparer).Count();
}


利用HashSet没有重复条目的事实:您可以通过获取总数的差异来对它们共享的条目求和两个Hashsets中的项目数量并减去其Union中的项目数:

 HashSet< string> hs1 =  new  HashSet< string> 
{
m0 m1 m2 m7
};

HashSet< string> hs2 = new HashSet< string>
{
m0 m2 m7
};

private int GetSharedMovieCount(HashSet< string> h1,HashSet< string> h2)
{
return h1.Count + h2.Count - (h1.Union(h2).Count());
}

// 在某些Method或EventHandler中测试
Dictionary< string,HashSet< string>> movies = new Dictionary< string,HashSet< string>>
{
{ movie1,hs1},
{ movie2,hs2}
};

int cnt = GetSharedMovieCount(movies [ movie1],电影[ movie2]); // => 3


So I have a dictionary for which each key can take more than one value (i.e Dictionary < string, Hashset < string > > ) And I have filled my Dictionary like this:

Key  Values

A    movie 0, movie 1, movie 2, movie 7
B    movie 0, movie 2, movie 7

And I have a counter that should count for each match there is in the values between these two keys (i.e key A, Key B), so in that case the counter should return 3, Is there a way to loop on the values of each key and every time it finds a match, it increments the counter?

解决方案

Sounds fairly simple:

public static int CountCommonValues<TKey, TValue>(
    this IDictionary<TKey, HashSet<TValue>> dictionary, 
    TKey keyA, TKey keyB, IEqualityComparer<TValue> comparer = null)
{
    if (dictionary == null) throw new ArgumentNullException("dictionary");
    
    HashSet<TValue> valuesA, valuesB;
    if (!dictionary.TryGetValue(keyA, out valuesA)) return 0;
    if (!dictionary.TryGetValue(keyB, out valuesB)) return 0;
    if (valuesA == null || valuesB == null) return 0;
    
    return valuesA.Intersect(valuesB, comparer).Count();
}


Taking advantage of the fact that a HashSet has no duplicate entries: you can sum the entries they share by taking the difference of the total count of items in two Hashsets and subtract the number of items in their 'Union:

HashSet<string> hs1 = new HashSet<string>
{
    "m0","m1","m2","m7"
};

HashSet<string> hs2 = new HashSet<string>
{
    "m0","m2","m7"
};

private int GetSharedMovieCount(HashSet<string> h1, HashSet<string> h2)
{
    return h1.Count + h2.Count - (h1.Union(h2).Count());
}

// test in some Method or EventHandler
Dictionary<string, HashSet<string>> movies = new Dictionary<string, HashSet<string>>
{
     {"movie1", hs1},
     {"movie2", hs2}
};

int cnt = GetSharedMovieCount(movies["movie1"], movies["movie2"]); // => 3


这篇关于如何比较一个字典的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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