简体   繁体   English

IEnumerable的 <string> 到字典 <char, IEnumerable<string> &gt;

[英]IEnumerable<string> to Dictionary<char, IEnumerable<string>>

I suppose that this question might partially duplicate other similar questions, but i'm having troubles with such a situation: 我想这个问题可能会部分重复其他类似的问题,但是我在这种情况下遇到了麻烦:

I want to extract from some string sentences 我想从一些字符串句子中提取

For example from 例如来自

`string sentence = "We can store these chars in separate variables. We can also test against other string characters.";`

I want to build an IEnumerable words; 我想建立一个IEnumerable的单词;

var separators = new[] {',', ' ', '.'};

IEnumerable<string> words = sentence.Split(separators, StringSplitOptions.RemoveEmptyEntries);

After that, go throught all these words and take firs character into a distinct ascending ordered collection of characters. 在那之后,遍历所有这些words ,并将冷杉字符带入一个独特的升序字符集合中。

var firstChars = words.Select(x => x.ToCharArray().First()).OrderBy(x => x).Distinct();

After that, go through both collections and for each character in firstChars get all items from words which has the first character equal with current character and create a Dictionary<char, IEnumerable<string>> dictionary . 之后,遍历两个集合,并为firstChars每个字符获取words中具有与current character相等的first character所有items ,并创建一个Dictionary<char, IEnumerable<string>> dictionary

I'm doing this way: 我这样做:

var dictionary = (from k in firstChars
                  from v in words
                  where v.ToCharArray().First().Equals(k)
                  select new { k, v })
                  .ToDictionary(x => x);

and here is the problem: An item with the same key has already been added. 这是问题所在: An item with the same key has already been added. Whis is because into that dictionary It is going to add an existing character. Whis是因为要在该字典中添加现有字符。

I included a GroupBy extension into my query 我在查询中加入了GroupBy扩展

var dictionary = (from k in firstChars
                  from v in words
                  where v.ToCharArray().First().Equals(k)
                  select new { k, v })
                  .GroupBy(x => x)
                  .ToDictionary(x => x);

The solution above gives makes all OK, but it gives me other type than I need. 上面的解决方案可以使一切正常,但是它可以提供我所需的其他类型。

在此处输入图片说明 What I should do to get as result an Dictionary<char, IEnumerable<string>>dictionary but not Dictionary<IGouping<'a,'a>> ? 我应该怎么做才能得到 Dictionary<char, IEnumerable<string>>dictionary 而不是 Dictionary<IGouping<'a,'a>>

The result which I want is as in the bellow image: 我想要的结果如下图所示: 在此处输入图片说明 But here I have to iterate with 2 foreach(s) which will Show me wat i want... I cannot understand well how this happens ... 但是在这里我必须用2个foreach进行迭代,这将向我显示我想要的wat ...我不明白这是如何发生的...

Any suggestion and advice will be welcome. 任何建议都将受到欢迎。 Thank you. 谢谢。

As the relation is one to many, you can use a lookup instead of a dictionary: 由于该关系是一对多的,因此可以使用查找代替字典:

var lookup = words.ToLookup(word => word[0]);

loopkup['s'] -> store, separate... as an IEnumerable<string>

And if you want to display the key/values sorted by first char: 如果要显示按第一个字符排序的键/值,请执行以下操作:

for (var sortedEntry in lookup.OrderBy(entry => entry.Key))
{
  Console.WriteLine(string.Format("First letter: {0}", sortedEntry.Key);
  foreach (string word in sortedEntry)
  {
    Console.WriteLine(word);
  }
}

You can do this: 你可以这样做:

var words = ...
var dictionary = words.GroupBy(w => w[0])
                      .ToDictionary(g => g.Key, g => g.AsEnumerable());

But for matter, why not use an ILookup ? 但是,为什么不使用ILookup呢?

var lookup = words.ToLookup(w => w[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM