在哈希集字典中查找值的组合
Finding the combinations of values in a dictionary of hashsets
我有一个 Dictionary<string, HashSet<string>>
我想遍历并找到 HashSet 中所有可能的组合,而无需任何重复。例如(虚拟数据):
Keys Values (HashSet)
"greeting" "Hello" "Hi" "Hey" "Howdy" ...
"Name" "Tom" "Angel" "Edward" ...
"question" "how are you?" "can you help me?" "is everything okay?" ...
... ...
... ....
我希望输出是某种集合,每个值为:
"Hello Tom how are you?"
"Hello Tom can you help me?"
"Hello Tom is everything okay?"
"Hello Angel how are you?"
"Hello Angel can you help me?"
"Hello Angel is everything okay?"
"Hello Edward how are you?"
...
"Hi Tom how are you?"
"Hi Tom can you help me?"
"Hi Tom is everything okay?"
"Hi Angel how are you?"
...
我希望它尽可能抽象,以便我可以根据需要在哈希集中添加尽可能多的键和值。
我试过递归地做,但我不太擅长,我无法想出一个基本案例......我认为你可以用 linq 来做,但我对 linq 一点都不熟悉。
我使用 Dictionary<string, HashSet<string>>
的原因是因为我收集信息的方式,所以我想保留这个数据结构。
谢谢!
一个简单的解决方案是...
Dictionary<string, HashSet<string>> test = new Dictionary<string, HashSet<string>>();
test.Keys.ToList().ForEach(key =>
{
test[key].ToList().ForEach(value => Console.WriteLine("key key:" + "value:" + value));
});
鉴于问题中提供的示例数据:
var data = new Dictionary<string, HashSet<string>>
{
{"greeting", new HashSet<string> {"Hello", "Hi", "Hey", "Howdy"}},
{"Name", new HashSet<string> {"Tom", "Angel", "Edward"}},
{"question", new HashSet<string> {"how are you?", "can you help me?", "is everything okay?"}}
};
没有任意键,这里有一个使用 LINQ 的简单方法:
var collection = from g in data["greeting"]
from n in data["Name"]
from q in data["question"]
select string.Format("{0} {1} {2}", g, n, q);
使用 this answer 中的 CartesianProduct
扩展方法将如下所示:
var collection = data.Select(x => x.Value).CartesianProduct().Select(x => x.Aggregate((a, b) => a + " " + b));
在任何一种情况下,这是我用来在控制台应用程序中显示输出的 foreach
:
foreach (var line in collection)
{
Console.WriteLine(line);
}
我有一个 Dictionary<string, HashSet<string>>
我想遍历并找到 HashSet 中所有可能的组合,而无需任何重复。例如(虚拟数据):
Keys Values (HashSet)
"greeting" "Hello" "Hi" "Hey" "Howdy" ...
"Name" "Tom" "Angel" "Edward" ...
"question" "how are you?" "can you help me?" "is everything okay?" ...
... ...
... ....
我希望输出是某种集合,每个值为:
"Hello Tom how are you?"
"Hello Tom can you help me?"
"Hello Tom is everything okay?"
"Hello Angel how are you?"
"Hello Angel can you help me?"
"Hello Angel is everything okay?"
"Hello Edward how are you?"
...
"Hi Tom how are you?"
"Hi Tom can you help me?"
"Hi Tom is everything okay?"
"Hi Angel how are you?"
...
我希望它尽可能抽象,以便我可以根据需要在哈希集中添加尽可能多的键和值。
我试过递归地做,但我不太擅长,我无法想出一个基本案例......我认为你可以用 linq 来做,但我对 linq 一点都不熟悉。
我使用 Dictionary<string, HashSet<string>>
的原因是因为我收集信息的方式,所以我想保留这个数据结构。
谢谢!
一个简单的解决方案是...
Dictionary<string, HashSet<string>> test = new Dictionary<string, HashSet<string>>();
test.Keys.ToList().ForEach(key =>
{
test[key].ToList().ForEach(value => Console.WriteLine("key key:" + "value:" + value));
});
鉴于问题中提供的示例数据:
var data = new Dictionary<string, HashSet<string>>
{
{"greeting", new HashSet<string> {"Hello", "Hi", "Hey", "Howdy"}},
{"Name", new HashSet<string> {"Tom", "Angel", "Edward"}},
{"question", new HashSet<string> {"how are you?", "can you help me?", "is everything okay?"}}
};
没有任意键,这里有一个使用 LINQ 的简单方法:
var collection = from g in data["greeting"]
from n in data["Name"]
from q in data["question"]
select string.Format("{0} {1} {2}", g, n, q);
使用 this answer 中的 CartesianProduct
扩展方法将如下所示:
var collection = data.Select(x => x.Value).CartesianProduct().Select(x => x.Aggregate((a, b) => a + " " + b));
在任何一种情况下,这是我用来在控制台应用程序中显示输出的 foreach
:
foreach (var line in collection)
{
Console.WriteLine(line);
}