C#将单个字典的值合并为一个扩展列表

时间:2015-08-19 13:46:52

标签: c# dictionary

这是一个场景:我有一个单词/建议术语词典。我正在尝试创建一个新值列表,结合每个单词的建议术语。 键的数量可以是变量(通常是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

提前谢谢。

2 个答案:

答案 0 :(得分:3)

您可以使用cartesian product by 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;
}

答案 1 :(得分:1)

有一种方法可以用Linq做到这一点,虽然我承认它是相当冗长的

SelectMany将IEnumerable的IEnumerable转换为单个IEnumerable。我们可以使用它,然后将其转换回字典

var desired = suggestions
    .SelectMany(s => s.Value 
                        .Select(suggest => new { s.Key, Word = suggest }))
    .ToDictionary(x => x.Key, y => y.Word);