组织包含多个条件的大量数据的最佳方法是什么?

时间:2015-06-17 07:26:22

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

所以我的“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.

现在,我不想用另一个名字制作3500lists。而且如果我想对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 个答案:

答案 0 :(得分: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,
}

答案 1 :(得分:0)

我会使用带有简单链接的自定义散列算法实现此目的,如果它的值相同则不进行探测。那将是紧张的方式。

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

只需返回List.Count()即可。

如果您需要有关散列的更多信息,请观看: https://www.youtube.com/watch?v=0M_kIqhwbFo

编辑:

阅读评论后,如果您只想生成所有排列,则应使用枚举或列出类别的可能值。然后根据此问题How to generate all permutations of a list in Python

将代码调整为c#