我想知道列表中每个项目的数量
I want to know the count of each item in a list
所以,我有一个投票列表。
List<string> Votes = new List<string>();
最多三个不同的字符串。我想对它们进行计数。这是在 c# 中。
我查看了之前的问题,但没有发现任何类似的问题,而且问题比我的复杂得多。
抱歉,如果这个问题已经得到回答
嗯,Linq 是这样的吗?:
List<String> votes = new List<String>() {
"Yes", "No", "Yes", "No", "?"
};
...
String report = String.Join(Environment.NewLine, votes
.GroupBy(item => item)
.Select(chunk => String.Format("{0} votes, count {1}", chunk.Key, chunk.Count()))
);
Console.Write(report);
您可以使用 GroupBy
:
var voteGroups = Votes.GroupBy(s => s);
现在,如果您想知道每个 string
的计数,请使用 Enumerable.Count
:
foreach(var voteGroup in voteGroups)
Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());
另一种方法是使用 ToLookup
:
var voteLookup = Votes.ToLookup(s => s);
foreach (var voteGroup in voteLookup)
Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());
查找有它的优势,它使您能够像字典一样查找特定元素。所以你可以通过这种方式得到 "pepsi"-count:
int pepsiCount = voteLookup["Pepsi"].Count();
如果列表中没有这样的字符串(计数将为 0),这不会导致异常。
如果你想让它不区分大小写,那么将"pepsi"和"Pepsi"视为相等的:
var voteLookup = Votes.ToLookup(s => s, StringComparer.InvariantCultureIgnoreCase);
如果您只有 List
个 strings
,那么您可以只使用 Count
属性。
List<string> Votes = new List<string>();
// populate list here
Console.WriteLine(Votes.Count);
试试这个...
var l = new List();
l.Add("abcd");
l.Add("abcd123");
l.Add("abcd1234");
var d = l.ToDictionary<string,int>(k=>k, v=>v.Length);
var count = d["abcd"]; //result would be 4.
如果您只是想获取列表中唯一项目的总数:
int count = (from x in lst select x).Distinct().Count();
不是最好的代码,但它有效:
int count1 = 0;
int count2 = 0;
foreach (String answer in Votes)
{
if (answer == "yes")
{
count1++;
}
if (answer == "no")
{
count2++;
}
}
所以,我有一个投票列表。
List<string> Votes = new List<string>();
最多三个不同的字符串。我想对它们进行计数。这是在 c# 中。
我查看了之前的问题,但没有发现任何类似的问题,而且问题比我的复杂得多。 抱歉,如果这个问题已经得到回答
嗯,Linq 是这样的吗?:
List<String> votes = new List<String>() {
"Yes", "No", "Yes", "No", "?"
};
...
String report = String.Join(Environment.NewLine, votes
.GroupBy(item => item)
.Select(chunk => String.Format("{0} votes, count {1}", chunk.Key, chunk.Count()))
);
Console.Write(report);
您可以使用 GroupBy
:
var voteGroups = Votes.GroupBy(s => s);
现在,如果您想知道每个 string
的计数,请使用 Enumerable.Count
:
foreach(var voteGroup in voteGroups)
Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());
另一种方法是使用 ToLookup
:
var voteLookup = Votes.ToLookup(s => s);
foreach (var voteGroup in voteLookup)
Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());
查找有它的优势,它使您能够像字典一样查找特定元素。所以你可以通过这种方式得到 "pepsi"-count:
int pepsiCount = voteLookup["Pepsi"].Count();
如果列表中没有这样的字符串(计数将为 0),这不会导致异常。
如果你想让它不区分大小写,那么将"pepsi"和"Pepsi"视为相等的:
var voteLookup = Votes.ToLookup(s => s, StringComparer.InvariantCultureIgnoreCase);
如果您只有 List
个 strings
,那么您可以只使用 Count
属性。
List<string> Votes = new List<string>();
// populate list here
Console.WriteLine(Votes.Count);
试试这个...
var l = new List();
l.Add("abcd");
l.Add("abcd123");
l.Add("abcd1234");
var d = l.ToDictionary<string,int>(k=>k, v=>v.Length);
var count = d["abcd"]; //result would be 4.
如果您只是想获取列表中唯一项目的总数:
int count = (from x in lst select x).Distinct().Count();
不是最好的代码,但它有效:
int count1 = 0;
int count2 = 0;
foreach (String answer in Votes)
{
if (answer == "yes")
{
count1++;
}
if (answer == "no")
{
count2++;
}
}