从具有常见值的字典中获取 TKey,其中 TValue 为 List<string>

我有一本看起来像这样的字典:


Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>()

{

    {"a" , new List<string> { "Red","Yellow"} },

    {"b" , new List<string> { "Blue","Red"} },

    {"c" , new List<string> { "Green","Orange"} },

    {"d" , new List<string> { "Black","Green"} },

};

我需要作为字典输出,dict其中的公共值List<string>应该是键,值应该是键列表。


例如:


Red: [a,b]

Green: [c,d]

我不知道如何用listin dictionaryas解决这个问题TValue。


请解释我如何处理字典中的列表。


RISEBY
浏览 73回答 2
2回答

杨__羊羊

您可以用扁平化您的字典SelectMany并获得看起来像的简单列表"a" - "Red""a" - "Yellow""b" - "Blue""b" = "Red"// and so on然后按值分组并从这些组中构建一个新字典。试试这个代码:var commonValues = dict.SelectMany(kv => kv.Value.Select(v => new {key = kv.Key, value = v}))&nbsp; &nbsp; .GroupBy(x => x.value)&nbsp; &nbsp; .Where(g => g.Count() > 1)&nbsp; &nbsp; .ToDictionary(g => g.Key, g => g.Select(x => x.key).ToList());

墨色风雨

很多循环......循环遍历字典,然后循环遍历列表中的每个值。var result = new Dictionary<string, List<string>>();// Loop through each key/value pair in the dictionaryforeach (var kvp in dict){&nbsp; &nbsp; // kvp.Key is the key ("a", "b", etc)&nbsp; &nbsp; // kvp.Value is the list of values ("Red", "Yellow", etc)&nbsp; &nbsp; // Loop through each of the values&nbsp; &nbsp; foreach (var value in kvp.Value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // See if our results dictionary already has an entry for this&nbsp; &nbsp; &nbsp; &nbsp; // value. If so, grab the corresponding list of keys. If not,&nbsp; &nbsp; &nbsp; &nbsp; // create a new list of keys and insert it.&nbsp; &nbsp; &nbsp; &nbsp; if (!result.TryGetValue(value, out var list))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.Add(value, list);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Add our key to this list of keys&nbsp; &nbsp; &nbsp; &nbsp; list.Add(kvp.Key);&nbsp; &nbsp; }}如果您想通过包含多个项目的条目来过滤它,那么您可以执行以下操作:result = result.Where(x => x.Value.Count > 1).ToDictionary(x => x.Key, x => x.Value);或者,您可以避免循环并改用 Linq:// Flatten the dictionary into a set of tuples// e.g. (a, Red), (a, Yellow), (b, Blue), (b, Red), etcvar result = dict.SelectMany(kvp => kvp.Value.Select(color => (key: kvp.Key, color)))&nbsp; &nbsp; // Group by the value, taking the color as the elements of the group&nbsp; &nbsp; // e.g. (Red, (a, b)), (Yellow, (a)), etc&nbsp; &nbsp; .GroupBy(item => item.color, item => item.key)&nbsp; &nbsp; // Filter to the ones with more than one item&nbsp; &nbsp; .Where(group => group.Count() > 1)&nbsp; &nbsp; // Turn it into a dictionary, taking the key of the grouping&nbsp; &nbsp; // (Red, Green, etc), as the dictionary key&nbsp; &nbsp; .ToDictionary(group => group.Key, group => group.ToList());您还可以使用 linq 查询语法,该语法稍长,但避免了SelectMany:var result =&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; from kvp in dict&nbsp; &nbsp; &nbsp; &nbsp; from color in kvp.Value&nbsp; &nbsp; &nbsp; &nbsp; group kvp.Key by color into grp&nbsp; &nbsp; &nbsp; &nbsp; where grp.Count() > 1&nbsp; &nbsp; &nbsp; &nbsp; select grp&nbsp; &nbsp; ).ToDictionary(grp => grp.Key, grp => grp.ToList());
打开App,查看更多内容
随时随地看视频慕课网APP