分组和计数项目

编程入门 行业动态 更新时间:2024-10-27 08:23:59
本文介绍了分组和计数项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个.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();

更多推荐

分组和计数项目

本文发布于:2023-11-22 08:39:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!