是否可以根据字符串中字符的频率创建一个数组?
本质上,我发现了一段旧的 LINQ C# 代码,它计算了某个字符串中最常见的字母。但是,我使用频率分析来解决已进行移位加密的解码文本,因此我希望它不仅返回最流行的字符,而且返回按出现频率排序的字符数组。
这是我在这里找到的 LINQ 代码:
input.GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key
Essentially, I found an old piece of LINQ C# code that counted the most frequent letter in a certain string. However, I'm using frequency analysis to solve a decoded text that has been shift-ciphered so I'm wanting it to return not just the most popular char, but a char array ordered by frequency of appearance.
Here is the LINQ code I found on here:
input.GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你已经拥有它了。
Well you pretty much have it already.
将
.First().Key
替换为.Select(group => group.Key)
应该会返回按频率降序排列的字符。Replacing
.First().Key
with.Select(group => group.Key)
should return to you the characters sorted by frequency in descending order.这是一个不使用 LINQ 的解决方案,无需学习 LINQ 也可以理解:
Here's a solution that doesn't use LINQ, which might be understandable without learning LINQ:
信息就在那里,只是不要扔掉它:
哦,对了,你只想要字符,而不是它们的频率。那么你必须扔掉一些信息:
The information is all there, just don't throw it away:
Oh, right, you just want the characters, not their frequency. Then you have to throw away some of the information: