如何计数存储在包含多个分钟的文件中的号码的出现? [英] How to count occurences of number stored in file containing multiple delimeters?

查看:80
本文介绍了如何计数存储在包含多个分钟的文件中的号码的出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在文件中的输入存储:

  50 | Carbon | Mercury | P:4; P:00; P :1 
90 | Oxygen | Mars | P:10; P:4; P:00
90 | Serium | Jupiter | P:4; P:16; P:10
85 | Hydrogen | Saturn | P:00; P:10; P:4

P:4 ,然后是下一个 P:00 ,然后按照明智的方式计数,并计入其他所有行,因此预期输出


P:4 3 (位于第2行,第3行第4行(最后一个单元格))



P:00 2 (位于第2行第4行)



P:1 0 (没有出现)



P:10 1
$ b

P:16 0


>



像我想要打印的每一个比例的出现。



到目前为止我是成功的拆分行并存储在我的类文件对象像这样:

  public class Planets 
{
//我的rest字段
public string ProportionConcat {get;组; }
public List< proportion> proportion {get;组; }
}

public class proportion
{
public int Number {get;组; }
}

>和最后,我的行星对象数据列表是这样的:

 列表< Planets = new List< Planets>(); 
行星[0]:
{
数量:50
名称:碳
物体:水星
比例连线:P:4; :1
proportion [0]:
{
Number:4
},
proportion [1]:
{
Number:00
},
proportion [2]:
{
Number:1
}
}
pre>

Etc ...



我知道我可以循环执行搜索和计数,但然后2到3循环将需要和代码将是一点杂乱,所以我想要一些更好的代码来执行此。



现在我如何搜索每个和在我的星球中计数其他比例List对象

解决方案

好吧,如果你已经解析了比例,输出数据:

  //存储结果分类
public class Values
{
public int Count; //比例条目计数。
public readonly HashSet< int> Rows = new HashSet< int>(); // list with rows numbers。

///< summary>添加新比例< / summary>
///< param name =rowNumber>行数,其中比例条目< / param>
public void Increment(int rowNumber)
{
++ Count; //增加比例条目数
Rows.Add(rowNumber); // add number of row,where proportion entry
}
}

使用这段代码填充它。我不确定它是凌乱,并没有看到必要使代码与LINQ复杂。

  var result = new Dictionary< int,Values>(); //创建字典,我们将存储我们的结果。键是比例的。值 - 关于这个比例条目和行的频率的信息,其中这个比例条目
用于(var i = 0; i {
var planet = Planets [i];
foreach(在planet.proportion中的var比例)
{
if(!result.ContainsKey(proportion.Number))//如果我们的结果字典不包含比例
result .Add(proportion.Number,new Values()); //我们将它添加到字典并初始化这个比例的结果类

result [proportion.Number] .Increment(i); //增加条目数并添加行号
}
}


This is my input store in file:

50|Carbon|Mercury|P:4;P:00;P:1
90|Oxygen|Mars|P:10;P:4;P:00
90|Serium|Jupiter|P:4;P:16;P:10
85|Hydrogen|Saturn|P:00;P:10;P:4

Now i will take my first row P:4 and then next P:00 and then next like wise and want to count occurence in every other row so expected output will be:

P:4 3(found in 2nd row,3rd row,4th row(last cell))

P:00 2 (found on 2nd row,4th row)

P:1 0 (no occurences are there so)

P:10 1

P:16 0

etc.....

Like wise i would like to print occurence of each and every proportion.

So far i am successfull in splitting row by row and storing in my class file object like this:

public class Planets
    {
       //My rest fields
        public string ProportionConcat { get; set; }
        public List<proportion> proportion { get; set; }
    }

    public class proportion
    {
        public int Number { get; set; } 
    }

I have already filled my planet object like below and Finally my List of planet object data is like this:

List<Planets> Planets = new List<Planets>();
Planets[0]:
    {
       Number:50
       name: Carbon
       object:Mercury
       ProportionConcat:P:4;P:00;P:1
       proportion[0]:
                 {
                     Number:4
                 },
        proportion[1]:
                 {
                     Number:00
                 },
    proportion[2]:
                 {
                     Number:1
                 }
    }

Etc...

I know i can loop through and perform search and count but then 2 to 3 loops will be required and code will be little messy so i want some better code to perform this.

Now how do i search each and count every other proportion in my planet List object??

解决方案

Well, if you have parsed proportions, you can create new struct for output data:

// Class to storage result
public class Values
{
    public int Count;   // count of proportion entry.
    public readonly HashSet<int> Rows = new HashSet<int>();  //list with rows numbers.

    /// <summary> Add new proportion</summary>
    /// <param name="rowNumber">Number of row, where proportion entries</param>
    public void Increment(int rowNumber)
    {
        ++Count;    // increase count of proportions entries
        Rows.Add(rowNumber);   // add number of row, where proportion entry
    }
}

And use this code to fill it. I'm not sure it's "messy" and don't see necessity to complicate the code with LINQ. What do you think about it?

   var result = new Dictionary<int, Values>();   // create dictionary, where we will storage our results. keys is proportion. values - information about how often this proportion entries and rows, where this proportion entry
   for (var i = 0; i < Planets.Count; i++)   // we use for instead of foreach for finding row number. i == row number
   {
      var planet = Planets[i];
      foreach (var proportion in planet.proportion)
      {
         if (!result.ContainsKey(proportion.Number))   // if our result dictionary doesn't contain proportion
            result.Add(proportion.Number, new Values());  // we add it to dictionary and initialize our result class for this proportion

         result[proportion.Number].Increment(i);  // increment count of entries and add row number
      }
   }

这篇关于如何计数存储在包含多个分钟的文件中的号码的出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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