【问题标题】:What is the best way to organize a lot of data which contains multiple conditions?组织大量包含多个条件的数据的最佳方法是什么?
【发布时间】:2015-06-17 07:26:22
【问题描述】:

所以我的“BIGLIST”中有很多包含多个条件的字符串,如下所示: 颜色、国家、城镇、好/坏、白天、早上/下午/晚上/晚上

有:

5 种颜色

5 个国家

5 镇

2 好/坏

7 天

4 早上/下午/晚上/晚上

所以,5*5*5*2*7*2 = 3500 种可能性

我的一些数据示例:

green england london good sunday evening

red thenetherlands amsterdam bad monday night

blue america newyork bad tuesday morning

所以现在我想将每一种可能性都分类到一个列表中。因此,如果您在我的 BIGLIST 中有 2 倍这种可能性:blue america newyork bad tuesday morning,则列表:“blueamericanewyorkbadtuesdaymorningList”.count 将返回 2。

现在,我不想创建 3500 个列表,或者使用另一个名称。而且,如果我想对 BIGLIST 进行排序,到目前为止,这是我的想法: 这是一个好方法吗?有更简单的方法吗?

List<string> colorlist = new List<string>();
colorlist[0] = "blue";
colorlist[1] = "red";
//etc
for (int i = 0;i<BIGLIST;i++)
{
   for (int j=0;j<colorlist.count;j++)
   {
       if(BIGLIST[i].contains(colorlist[j])) 
       {
          //etc
       }
   }
}

【问题讨论】:

  • 不太明白:为什么不创建一个具有“颜色”、“国家”、“城镇”等属性的类?
  • 2是你想要达到的结果吗?
  • List&lt;string&gt; colorlist = new List&lt;string&gt;(); colorlist[0] = "blue"; colorlist[1] = "red"; 这行不通.... 这不是 Javascript.. 您应该添加这些项目。 colorlist.Add("蓝色");
  • @Jeroen van Langen: List&lt;string&gt; colorlist = new List&lt;string&gt;() { "blue", "red" }; 工作(见collection initializer
  • @BinkanSalaryman 是的,那是另一种解决方案..

标签: c# list if-statement for-loop


【解决方案1】:

您可以按如下方式组织数据:

class Program {
    static void Main(string[] args) {
        List<Criteria> list = new List<Criteria>() {
            new Criteria(Color.green, Country.england, Town.london, GoodBad.good, DayOfTheWeek.sunday, Daytime.evening),
            new Criteria(Color.red, Country.thenetherlands, Town.amsterdam, GoodBad.bad, DayOfTheWeek.monday, Daytime.night),
            new Criteria(Color.blue, Country.america, Town.newyork, GoodBad.bad, DayOfTheWeek.tuesday, Daytime.morning),
        };


        Console.WriteLine("- Native sorting:");
        list.Sort();
        foreach(var criteria in list) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Color:");
        IOrderedEnumerable<Criteria> byColor = list.OrderBy(c => c.Color);
        foreach(var criteria in byColor) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Country:");
        IOrderedEnumerable<Criteria> byCountry = list.OrderBy(c => c.Country);
        foreach(var criteria in byCountry) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Town:");
        IOrderedEnumerable<Criteria> byTown = list.OrderBy(c => c.Town);
        foreach(var criteria in byTown) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Good:");
        IOrderedEnumerable<Criteria> byGood = list.OrderBy(c => c.GoodBad);
        foreach(var criteria in byGood) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By DayOfTheWeek:");
        IOrderedEnumerable<Criteria> byDayOfTheWeek = list.OrderBy(c => c.DayOfTheWeek);
        foreach(var criteria in byDayOfTheWeek) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Daytime:");
        IOrderedEnumerable<Criteria> byDaytime = list.OrderBy(c => c.Daytime);
        foreach(var criteria in byDaytime) {
            Console.WriteLine(criteria);
        }

        Console.ReadKey();
    }
}

sealed class Criteria : IComparable<Criteria> {
    public readonly Color Color;
    public readonly Country Country;
    public readonly Town Town;
    public readonly GoodBad GoodBad;
    public readonly DayOfTheWeek DayOfTheWeek;
    public readonly Daytime Daytime;

    public Criteria(Color color, Country country, Town town, GoodBad goodBad, DayOfTheWeek dayOfTheWeek, Daytime daytime) {
        this.Color = color;
        this.Country = country;
        this.Town = town;
        this.GoodBad = goodBad;
        this.DayOfTheWeek = dayOfTheWeek;
        this.Daytime = daytime;
    }

    public override int GetHashCode() {
        int result = (int)Color | (int)Country << 2 | (int)Town << 5 | (int)GoodBad << 8 | (int)DayOfTheWeek << 9 | (int)Daytime << 12;
        return result;
    }

    public override string ToString() {
        return string.Join(" ", Color, Country, Town, GoodBad, DayOfTheWeek, Daytime);
    }

    public int CompareTo(Criteria that) {
        int result = this.Color.CompareTo(that.Color);
        if(result != 0) {
            return result;
        }
        result = this.Country.CompareTo(that.Country);
        if(result != 0) {
            return result;
        }
        result = this.Town.CompareTo(that.Town);
        if(result != 0) {
            return result;
        }
        result = this.GoodBad.CompareTo(that.GoodBad);
        if(result != 0) {
            return result;
        }
        result = this.DayOfTheWeek.CompareTo(that.DayOfTheWeek);
        if(result != 0) {
            return result;
        }
        result = this.Daytime.CompareTo(that.Daytime);
        return result;
    }
}

//2 bits
enum Color {
    green,
    red,
    blue,
}

//3 bits
enum Country {
    england,
    thenetherlands,
    america,
}

//3 bits
enum Town {
    london,
    amsterdam,
    newyork,
}

//1 bit
enum GoodBad {
    good,
    bad,
}

//3 bits
enum DayOfTheWeek {
    monday,
    tuesday,
    wednesday,
    thursday,
    friday,
    saturday,
    sunday,
}

//3 bits
enum Daytime {
    morning,
    afternoon,
    evening,
    night,
}

【讨论】:

    【解决方案2】:

    我将使用具有简单链接的自定义哈希算法来实现这一点,如果它的值相同,则无需探测。那将是最快的方式。

    您可以开始使用枚举的索引号或字符串结果的长度等。

    只需返回 List.Count() 就可以了。

    如果你需要更多关于散列的信息,你应该看这个: https://www.youtube.com/watch?v=0M_kIqhwbFo

    编辑:

    阅读您的评论后,如果您只想生成所有排列,您应该创建枚举或列出一个类别的可能值。然后把这个问题How to generate all permutations of a list in Python的代码改成c#

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 2019-09-24
      • 2020-05-03
      • 2018-08-30
      • 2019-08-05
      • 2013-06-07
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多