计算字符串列表中的相等字符串并使它们唯一
Count equal strings in a list of string and make them unique
我有一个单词列表,例如 bi-gram:
welcome guys
guys and
and ladies
ladies repeat
repeat welcome
welcome guys
现在我想计算相等的字符串,return 得到这个输出:
welcome guys, 2
guys and, 1
and ladies, 1
ladies repeat, 1
repeat welcome, 1
我如何在 C# 中执行此操作?
这可以通过 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());
}
welcome guys, 2
guys and, 1
and ladies, 1
ladies repeat, 1
repeat welcome, 1
我有一个单词列表,例如 bi-gram:
welcome guys
guys and
and ladies
ladies repeat
repeat welcome
welcome guys
现在我想计算相等的字符串,return 得到这个输出:
welcome guys, 2
guys and, 1
and ladies, 1
ladies repeat, 1
repeat welcome, 1
我如何在 C# 中执行此操作?
这可以通过 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());
}
welcome guys, 2
guys and, 1
and ladies, 1
ladies repeat, 1
repeat welcome, 1