[英]Getting TKey from a dictionary with common values where TValue is List<string>
I have a dictionary which looks like this: 我有一个字典,看起来像这样:
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"} },
};
I need output as a dictionary from the dict
where common value in the List<string>
should be the key and the value should be the list of keys. 我需要输出作为字典来自
dict
,其中List<string>
中的公共值应该是键,值应该是键列表。
Eg: 例如:
Red: [a,b]
Green: [c,d]
I don't know how to solve this problem with list
in dictionary
as TValue
. 我不知道如何用
dictionary
list
作为TValue
来解决这个问题。
Please explain me how to handle lists with in dictionary. 请解释我如何处理字典中的列表。
You can flattern your dictionary with SelectMany
and get plain list that looks like 您可以使用
SelectMany
平面化您的字典并获得看起来像的简单列表
"a" - "Red"
"a" - "Yellow"
"b" - "Blue"
"b" = "Red"
// and so on
then group by value and build a new dictionary from those groups. 然后按值分组并从这些组中构建新的字典。 Try this code:
试试这段代码:
var commonValues = dict.SelectMany(kv => kv.Value.Select(v => new {key = kv.Key, value = v}))
.GroupBy(x => x.value)
.Where(g => g.Count() > 1)
.ToDictionary(g => g.Key, g => g.Select(x => x.key).ToList());
Lots of looping... Loop over the dictionary, then loop over each value in the list. 大量循环...遍历字典,然后循环遍历列表中的每个值。
var result = new Dictionary<string, List<string>>();
// Loop through each key/value pair in the dictionary
foreach (var kvp in dict)
{
// kvp.Key is the key ("a", "b", etc)
// kvp.Value is the list of values ("Red", "Yellow", etc)
// Loop through each of the values
foreach (var value in kvp.Value)
{
// See if our results dictionary already has an entry for this
// value. If so, grab the corresponding list of keys. If not,
// create a new list of keys and insert it.
if (!result.TryGetValue(value, out var list))
{
list = new List<string>();
result.Add(value, list);
}
// Add our key to this list of keys
list.Add(kvp.Key);
}
}
If you want to filter this by entries which have more than one item, then you can do: 如果要通过具有多个项目的条目对其进行过滤,则可以执行以下操作:
result = result.Where(x => x.Value.Count > 1).ToDictionary(x => x.Key, x => x.Value);
Alternatively, you can avoid loops and use Linq instead: 或者,您可以避免循环并使用Linq:
// Flatten the dictionary into a set of tuples
// e.g. (a, Red), (a, Yellow), (b, Blue), (b, Red), etc
var result = dict.SelectMany(kvp => kvp.Value.Select(color => (key: kvp.Key, color)))
// Group by the value, taking the color as the elements of the group
// e.g. (Red, (a, b)), (Yellow, (a)), etc
.GroupBy(item => item.color, item => item.key)
// Filter to the ones with more than one item
.Where(group => group.Count() > 1)
// Turn it into a dictionary, taking the key of the grouping
// (Red, Green, etc), as the dictionary key
.ToDictionary(group => group.Key, group => group.ToList());
You can also use linq query syntax, which is slightly longer but avoids the mess around the SelectMany
: 你也可以使用linq查询语法,这个语法稍微长一点,但避免了
SelectMany
周围的混乱:
var result =
(
from kvp in dict
from color in kvp.Value
group kvp.Key by color into grp
where grp.Count() > 1
select grp
).ToDictionary(grp => grp.Key, grp => grp.ToList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.