分组和计数项目 [英] Group and count items

查看:69
本文介绍了分组和计数项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件(words.csv),其中包含以逗号分隔的5000个单词.大多数字符串是重复值.

I have a .csv file (words.csv) containing 5000 words seperated by commas. Most of the strings are repeated values.

我可以使用LINQ执行以下操作吗?

Can I use LINQ to do the following:

A.将常见单词归为一组,并显示重复单词的数量

A. group common words together and show count of repeated words

因此,如果苹果已重复5次,香蕉已重复3次,则应显示为

so if apple has been repeated 5 times and banana 3 times..it should display as

苹果-5
香蕉-3 等等

apple - 5
banana - 3 and so on

B.创建另一个删除了重复项的文本文件.

B. Create another text file with duplicates removed.

推荐答案

当然,这是C#中的LINQ语法:

Sure, here's the LINQ syntax in C#:

from word in words
group word into occurrences
select new
{
    Word = occurrences.Key,
    Count = occurrences.Count()
}

或在纯" C#方法调用中:

Or in "pure" C# method calls:

words.GroupBy(w => w)
     .Select(o => new 
                  { 
                     Word = o.Key,
                     Count = o.Count()
                  });

要创建单词的独特列表,您只需使用Distinct运算符:

And to create a distinct list of words you just use the Distinct operator:

words.Distinct();

这篇关于分组和计数项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆