【发布时间】:2017-04-18 12:31:55
【问题描述】:
我需要帮助。 我需要找到在text1中但不在text2中的单词,还要计算它们出现的次数。
例子:
Text1(你好,世界苹果,菠萝,卷心菜,苹果)
Text2(你好,世界,菠萝)
结果:
苹果 2;
卷心菜1;
如果没有 List 也很棒
【问题讨论】:
-
为什么“世界苹果”对“苹果”的计数有贡献?
我需要帮助。 我需要找到在text1中但不在text2中的单词,还要计算它们出现的次数。
例子:
Text1(你好,世界苹果,菠萝,卷心菜,苹果)
Text2(你好,世界,菠萝)
结果:
苹果 2;
卷心菜1;
如果没有 List 也很棒
【问题讨论】:
您可以使用两个数组,然后使用Group By,您可以通过这种方式实现您的目标:
string[] text1 = new []{"hello", "world", "apple", "pineapple", "cabbage", "apple"};
string[] text2 = new []{"apple", "pineapple", "cabbage", "apple"};
string[] combinedText = text1.Concat(text2).ToArray();
var groups = combinedText.GroupBy(v => v);
foreach(var group in groups)
Console.WriteLine("Value {0} has {1} items", group.Key, group.Count());
编辑:
看起来你想要的解决方案略有不同,所以我也在下面展示:
string[] text1 = new []{"hello", "world", "apple", "pineapple", "cabbage", "apple"};
string[] text2 = new []{"apple", "pineapple", "cabbage", "apple"};
var text1Groups = text1.GroupBy(v => v);
var text2Groups = text2.GroupBy(v => v);
foreach(var group in text1Groups)
Console.WriteLine(group.Key.ToString() + group.Count().ToString());
foreach(var group in text2Groups)
Console.WriteLine(group.Key.ToString() + group.Count().ToString());
【讨论】:
string text1 = "hello, world apple,pineapple,cabbage,apple";
string text2 = "hello, world,pineapple";
string pattern = @"\p{L}+";
var list1 = Regex.Matches(text1, pattern).Cast<Match>().Select(x => x.Value);
var list2 = Regex.Matches(text2, pattern).Cast<Match>().Select(x => x.Value);
var result = list1.Where(x => !list2.Contains(x))
.GroupBy(x => x)
.Select(x =>new
{
Word = x.Key,
Count= x.Count()
})
.ToList();
这将返回
Word = apple, Count = 2
Word = cabbage, Count = 1
当然还有一些性能改进的空间,但为了清楚起见,我们将把它们排除在外......
【讨论】: