按等级顺序显示文本文件中的行

本文关键字:文件 文本 显示 顺序 | 更新日期: 2023-09-27 18:18:18

我有一个文本文件,其中包含几行,其中许多是重复的。

我想描绘一个列表,其中出现次数最多的出现在顶部,出现次数最少的出现在底部。

但是,我想显示字符串在列表中出现在它旁边的次数。

我该怎么做呢?

按等级顺序显示文本文件中的行

一种快速简便的方法是使用Dictionary和循环:

using(StreamReader sr = new StreamReader("my file")) {
    Dictionary<string, int> items = new Dictionary<string, int>();
    while(sr.BaseStream.Position < sr.BaseStream.Length) {
        string s = sr.ReadLine();
        if(items.ContainsKey(s)) {
            items[s]++;
        } else {
            items.Add(s, 1);
        }
    }
    // You now have a dictionary of unique strings and their counts - you can sort it however you need.
}

如果文件不是太大,也就是说,如果它可以放在内存中,你可以将它存储在字典中。

创建一个包含"line of text" -> "number of it's been seen"的字典

每次一行读取文件。如果该行已经在字典中,则将字典值增加1。如果该行是新的,将其添加到字典中并将值设置为1。

读取整个文件后,可以取出键/值。按值排序以查找出现次数最多的值并打印结果。

.NET Framework 3.0的代码:

using System;
using System.IO;
using System.Collections.Generic;
public class Program
{
  private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string, int> kv2)
  {
    return kv2.Value == kv1.Value ? kv1.Key.CompareTo(kv2.Key) : kv2.Value - kv1.Value;
  }
  public static void Main()
  {
    Dictionary<string, int> histogram = new Dictionary<string, int>();
    using (StreamReader reader = new StreamReader("Test.txt"))
    {
      string line;
      while ((line = reader.ReadLine()) != null)
      {
        if (histogram.ContainsKey(line))
          ++histogram[line];
        else
          histogram.Add(line, 1);
      }
    }
    List<KeyValuePair<string, int>> sortedHistogram = new List<KeyValuePair<string, int>>(histogram);
    sortedHistogram.Sort(Compare);
    foreach (KeyValuePair<string, int> kv in sortedHistogram)
      Console.WriteLine("{0}'t{1}", kv.Value, kv.Key);
  }
}

用法:

ddd
aaa
ccc
bbb
aaa
aaa
bbb
输出:

3   aaa
2   bbb
1   ccc
1   ddd