C# 建立一个列表中项目计数的字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/687313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building a dictionary of counts of items in a list
提问by Ryan Ische
I have a List containing a bunch of strings that can occur more than once. I would like to take this list and build a dictionary of the list items as the key and the count of their occurrences as the value.
我有一个包含一堆可以多次出现的字符串的列表。我想使用这个列表并构建一个列表项的字典作为键,并将它们出现的次数作为值。
Example:
例子:
List<string> stuff = new List<string>();
stuff.Add( "Peanut Butter" );
stuff.Add( "Jam" );
stuff.Add( "Food" );
stuff.Add( "Snacks" );
stuff.Add( "Philosophy" );
stuff.Add( "Peanut Butter" );
stuff.Add( "Jam" );
stuff.Add( "Food" );
and the result would be a Dictionary containing:
结果将是一个包含以下内容的字典:
"Peanut Butter", 2
"Jam", 2
"Food", 2
"Snacks", 1
"Philosophy", 1
I have a way to do this, but it doesn't seem like I'm utilizing the good stuff in C# 3.0
我有办法做到这一点,但似乎我没有利用 C# 3.0 中的好东西
public Dictionary<string, int> CountStuff( IList<string> stuffList )
{
Dictionary<string, int> stuffCount = new Dictionary<string, int>();
foreach (string stuff in stuffList) {
//initialize or increment the count for this item
if (stuffCount.ContainsKey( stuff )) {
stuffCount[stuff]++;
} else {
stuffCount.Add( stuff, 1 );
}
}
return stuffCount;
}
采纳答案by casperOne
You can use the group clause in C# to do this.
您可以使用 C# 中的 group 子句来执行此操作。
List<string> stuff = new List<string>();
...
var groups = from s in stuff group s by s into g select
new { Stuff = g.Key, Count = g.Count() };
You can call the extension methods directly as well if you want:
如果需要,您也可以直接调用扩展方法:
var groups = stuff.GroupBy(s => s).Select(
s => new { Stuff = s.Key, Count = s.Count() });
From here it's a short hop to place it into a Dictionary<string, int>
:
从这里开始将它放入一个很短的跳Dictionary<string, int>
:
var dictionary = groups.ToDictionary(g => g.Stuff, g => g.Count);
回答by MarkusQ
One idea would be to give the dictionary a default valueof zero, so you wouldn't have to special case the first occurrence.
一个想法是给字典一个默认值零,这样你就不必特例第一次出现。
回答by Guffa
Well, there isn't really any better way to do it.
嗯,真的没有更好的方法来做到这一点。
Perhaps you could write a LINQ query that would group the strings and then count how many strings there are in each group, but that would not be nearly as efficient as what you already have.
也许您可以编写一个 LINQ 查询来对字符串进行分组,然后计算每个组中有多少个字符串,但这不会像您已有的那样有效。
回答by sfossen
I would have made a specialized List, that backed by the Dictionary and the add method would test for membership and increase count if found.
我会制作一个专门的 List,它由 Dictionary 支持,并且 add 方法将测试成员资格并在找到时增加计数。
sorta like:
有点像:
public class CountingList
{
Dictionary<string, int> countingList = new Dictionary<string, int>();
void Add( string s )
{
if( countingList.ContainsKey( s ))
countingList[ s ] ++;
else
countingList.Add( s, 1 );
}
}
回答by Aryan Firouzian
Dictionary<string, int> a = stuff.GroupBy(p => p).OrderByDescending(r=>r.Count()).ToDictionary(q => q.Key, q => q.Count());
You can GroupBy and then create dictionary to count each group. As performance testindicate, usually there are more efficient approaches other than Linq. I think your code is more efficient, while Linq solution is more readable and beautiful.
您可以 GroupBy 然后创建字典来计算每个组。正如性能测试表明的那样,除了 Linq 之外,通常还有更有效的方法。我认为您的代码更高效,而 Linq 解决方案更具可读性和美观性。