c#将单个字典的值组合成单个扩展列表
本文关键字:单个 扩展 组合 列表 字典 | 更新日期: 2023-09-27 18:08:48
场景如下:我有一个单词/建议术语字典。我正在尝试创建一个新值列表,将每个单词的建议术语组合在一起。键的数量可以是可变的(通常是1-3个字):
var suggestions = new Dictionary<string, List<SuggestItem>>();
其中SuggestItem
包含public string Term {get;set;}
算法的工作方式是给出一个句子,单词被分开,每个单词被检查并展开,如果是缩写,然后通过拼写检查器,它返回一个建议列表。这些结果以原始单词作为键存储在字典中。这部分都很好。我需要帮助的是把字典变成一个建议列表,其中包含建议术语的每种可能组合。
的例子:
给定一个句子:
"e cleve"
生成字典:
Keys: Values:
east east
eastlake
cleve cleves
college
cleveland
coolville
cloverdale
cedarville
celeryville
circleville
conesville
centerville
clairsville
clarksville
chesterville
cridersville
我需要返回的是:
Terms
east cleves
east college
east cleveland
east coolville
east cloverdale
east cedarville
east celeryville
east circleville
east conesville
east centerville
east clairsville
east clarksville
east chesterville
east cridersville
eastlake cleves
eastlake college
eastlake cleveland
eastlake coolville
eastlake cloverdale
eastlake cedarville
eastlake celeryville
eastlake circleville
eastlake conesville
eastlake centerville
eastlake clairsville
eastlake clarksville
eastlake chesterville
eastlake cridersville
作为List<string>()
注意:我可以为2个键硬编码,但是需要可变数量的键。
注2:我为与预期结果的歧义道歉。我希望我的编辑能澄清这一点。
如果在句子中有3个词:"e cleve commons"
,它将在建议字典中有3个键:
Keys: Values:
east east
eastlake
cleve cleves
college
cleveland
coolville
cloverdale
cedarville
celeryville
circleville
conesville
centerville
clairsville
clarksville
chesterville
cridersville
commons collins
corners
congress
columbus
conesville
crooksville
和结果列表如下:
Terms
east cleves collins
east college collins
east cleveland collins
east coolville collins
east cloverdale collins
east cedarville collins
east celeryville collins
east circleville collins
east conesville collins
east centerville collins
east clairsville collins
east clarksville collins
east chesterville collins
east cridersville collins
eastlake cleves collins
eastlake college collins
eastlake cleveland collins
eastlake coolville collins
eastlake cloverdale collins
eastlake cedarville collins
eastlake celeryville collins
eastlake circleville collins
eastlake conesville collins
eastlake centerville collins
eastlake clairsville collins
eastlake clarksville collins
eastlake chesterville collins
eastlake cridersville collins
east cleves collins
east college collins
east cleveland collins
east coolville collins
east cloverdale collins
east cedarville collins
east celeryville collins
east circleville collins
east conesville collins
east centerville collins
east clairsville collins
east clarksville collins
east chesterville collins
east cridersville collins
eastlake cleves collins
eastlake college collins
eastlake cleveland collins
eastlake coolville collins
eastlake cloverdale collins
eastlake cedarville collins
eastlake celeryville collins
eastlake circleville collins
eastlake conesville collins
eastlake centerville collins
eastlake clairsville collins
eastlake clarksville collins
eastlake chesterville collins
eastlake cridersville collins
east cleves corners
east college corners
east cleveland corners
east coolville corners
east cloverdale corners
east cedarville corners
east celeryville corners
east circleville corners
east conesville corners
east centerville corners
east clairsville corners
east clarksville corners
east chesterville corners
east cridersville corners
eastlake cleves corners
eastlake college corners
eastlake cleveland corners
eastlake coolville corners
eastlake cloverdale corners
eastlake cedarville corners
eastlake celeryville corners
eastlake circleville corners
eastlake conesville corners
eastlake centerville corners
eastlake clairsville corners
eastlake clarksville corners
eastlake chesterville corners
eastlake cridersville corners
east cleves congress
east college congress
east cleveland congress
east coolville congress
east cloverdale congress
east cedarville congress
east celeryville congress
east circleville congress
east conesville congress
east centerville congress
east clairsville congress
east clarksville congress
east chesterville congress
east cridersville congress
eastlake cleves congress
eastlake college congress
eastlake cleveland congress
eastlake coolville congress
eastlake cloverdale congress
eastlake cedarville congress
eastlake celeryville congress
eastlake circleville congress
eastlake conesville congress
eastlake centerville congress
eastlake clairsville congress
eastlake clarksville congress
eastlake chesterville congress
eastlake cridersville congress
east cleves columbus
east college columbus
east cleveland columbus
east coolville columbus
east cloverdale columbus
east cedarville columbus
east celeryville columbus
east circleville columbus
east conesville columbus
east centerville columbus
east clairsville columbus
east clarksville columbus
east chesterville columbus
east cridersville columbus
eastlake cleves columbus
eastlake college columbus
eastlake cleveland columbus
eastlake coolville columbus
eastlake cloverdale columbus
eastlake cedarville columbus
eastlake celeryville columbus
eastlake circleville columbus
eastlake conesville columbus
eastlake centerville columbus
eastlake clairsville columbus
eastlake clarksville columbus
eastlake chesterville columbus
eastlake cridersville columbus
east cleves conesville
east college conesville
east cleveland conesville
east coolville conesville
east cloverdale conesville
east cedarville conesville
east celeryville conesville
east circleville conesville
east conesville conesville
east centerville conesville
east clairsville conesville
east clarksville conesville
east chesterville conesville
east cridersville conesville
eastlake cleves conesville
eastlake college conesville
eastlake cleveland conesville
eastlake coolville conesville
eastlake cloverdale conesville
eastlake cedarville conesville
eastlake celeryville conesville
eastlake circleville conesville
eastlake conesville conesville
eastlake centerville conesville
eastlake clairsville conesville
eastlake clarksville conesville
eastlake chesterville conesville
eastlake cridersville conesville
east cleves crooksville
east college crooksville
east cleveland crooksville
east coolville crooksville
east cloverdale crooksville
east cedarville crooksville
east celeryville crooksville
east circleville crooksville
east conesville crooksville
east centerville crooksville
east clairsville crooksville
east clarksville crooksville
east chesterville crooksville
east cridersville crooksville
eastlake cleves crooksville
eastlake college crooksville
eastlake cleveland crooksville
eastlake coolville crooksville
eastlake cloverdale crooksville
eastlake cedarville crooksville
eastlake celeryville crooksville
eastlake circleville crooksville
eastlake conesville crooksville
eastlake centerville crooksville
eastlake clairsville crooksville
eastlake clarksville crooksville
eastlake chesterville crooksville
eastlake cridersville crooksville
提前感谢。
你可以使用Eric Lippert的笛卡尔积
var result = CartesianProduct(dict.Values.Select(x => x.Term))
.Select(x => String.Join(" ", x))
.ToList();
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
// base case:
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach (var sequence in sequences)
{
var s = sequence; // don't close over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}
有一种方法可以用Linq做到这一点,尽管我承认它相当冗长
SelectMany将IEnumerables的IEnumerable转换为单个IEnumerable。我们可以使用这个,然后将它转换回Dictionary
var desired = suggestions
.SelectMany(s => s.Value
.Select(suggest => new { s.Key, Word = suggest }))
.ToDictionary(x => x.Key, y => y.Word);