是否可以根据字符串中字符的频率创建一个数组?

发布于 2024-12-18 07:37:54 字数 235 浏览 2 评论 0原文

本质上,我发现了一段旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千仐 2024-12-25 07:37:54

好吧,你已经拥有它了。

input.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(x => x.Key).ToArray();

Well you pretty much have it already.

input.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(x => x.Key).ToArray();
生来就爱笑 2024-12-25 07:37:54

.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.

扛起拖把扫天下 2024-12-25 07:37:54

这是一个不使用 LINQ 的解决方案,无需学习 LINQ 也可以理解:

// count all the frequencies
var frequencies = new Dictionary<char, int>;
foreach(char c in input)
{
    if(frequencies.ContainsKey(c))
    {
        frequencies[c]++;
    }
    else
    {
        frequencies.Add(c, 1);
    }
}
// Get the characters
var characters = new List<char>(frequencies.Keys);
// Sort them
characters.Sort((x, y) => frequencies[x].CompareTo(frequencies[y]));

Here's a solution that doesn't use LINQ, which might be understandable without learning LINQ:

// count all the frequencies
var frequencies = new Dictionary<char, int>;
foreach(char c in input)
{
    if(frequencies.ContainsKey(c))
    {
        frequencies[c]++;
    }
    else
    {
        frequencies.Add(c, 1);
    }
}
// Get the characters
var characters = new List<char>(frequencies.Keys);
// Sort them
characters.Sort((x, y) => frequencies[x].CompareTo(frequencies[y]));
居里长安 2024-12-25 07:37:54
input.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(group => group.Key).ToArray();
input.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(group => group.Key).ToArray();
淡淡の花香 2024-12-25 07:37:54

信息就在那里,只是不要扔掉它:

Dictionary<char, int> count =
  input.GroupBy(g => g).ToDictionary(g => g.Key, g => g.Count());

哦,对了,你只想要字符,而不是它们的频率。那么你必须扔掉一些信息:

char[] chars =
  input.GroupBy(g => g).OrderByDescending(g => g.Count()).Select(g => g.Key)
  .ToArray();

The information is all there, just don't throw it away:

Dictionary<char, int> count =
  input.GroupBy(g => g).ToDictionary(g => g.Key, g => g.Count());

Oh, right, you just want the characters, not their frequency. Then you have to throw away some of the information:

char[] chars =
  input.GroupBy(g => g).OrderByDescending(g => g.Count()).Select(g => g.Key)
  .ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文