我有一个以bi-gram为例的单词列表:
welcome guys
guys and
and ladies
ladies repeat
repeat welcome
welcome guys
现在我想计算相等的字符串并返回获取此输出:
welcome guys, 2
guys and, 1
and ladies, 1
ladies repeat, 1
repeat welcome, 1
我怎样才能在c#中做到这一点?
答案 0 :(得分:0)
这可以通过Linq和GroupBy
函数轻松完成:
var input = new string[]
{
"welcome guys",
"guys and",
"and ladies",
"ladies repeat",
"repeat welcome",
"welcome guys"
};
var groups =
input
.GroupBy(x => x);
foreach (var g in groups)
{
Console.WriteLine("{0}, {1}", g.Key, g.Count().ToString());
}
欢迎你们,2
伙计们,1 和女士们,1
女士们再说一遍,1 重复欢迎,1