根据数组元素对每个500个数组元素进行分组

时间:2012-11-13 10:43:02

标签: c# linq c#-4.0

我有一个2000个字符串的数组。字符串是:“艺术”,“经济”,“体育”和“政治”。我想对每个500个元素进行分组并得到它们的计数

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:4)

另一种解决方案:

var count = 0;
var dictionaries = 
    strings.GroupBy(s => count++ / 500)
           .Select(g => g.Distinct().ToDictionary(k => k, k => g.Count(s => s == k)))
           .ToList();

这将创建List<Dictionary<string, int>>。每个字典代表500个元素的数量(或者最后一个字典可能更少),其中键是字符串,值是字典表示的500个元素中字符串的出现次数。

无需对可能遇到的所有可能值进行硬编码。

为了获得最大可能的性能,您还可以使用此版本:

var count = 0;
var dictionaries = 
    strings.GroupBy(s => count++ / 500)
           .Select(g => g.Aggregate(
               new Dictionary<string, int>(), 
               (d, w) => { d[w] = (d.ContainsKey(w) ? d[w] + 1 : 1); return d; })
           )
           .ToList();

此版本只对源数组中的每个元素进行一次迭代。输出格式与第一个版本相同。

答案 1 :(得分:3)

尝试

var grouping = Enumerable.Range(0,2000)
                       .Select(i => i / 500)
                       .Zip(Strings, (i,s) => new { Group = i, Str = s})
                       .GroupBy(anon => anon.Group,
                                anon => anon.Str,
                                (key,g) => new
                                           {
                                             Key = key,
                                             Art = g.Count(str => str == "art"),
                                             Economy = g.Count(str => str == "economy"),
                                             Politic = g.Count(str => str == "politic"),
                                             Sport= g.Count(str => str == "sport")
                                           });

foreach(anon in grouping)
{
    //textbox logic OP will have to change to suit
    TextBox1.WriteLine(String.Format("Group: {0}", anon.Key));
    TextBox1.WriteLine(String.Format("Art: {0}",anon.Art));
    TextBox1.WriteLine(String.Format("Economy: {0}",anon.Economy ));
    TextBox1.WriteLine(String.Format("Politic: {0}",anon.Politic ));
    TextBox1.WriteLine(String.Format("Sport: {0}",anon.Sport));
}

或者(根据Snowbear)

var grouping = Strings.Select((s,i) => new { Group = i / 500, Str = s})
                      .GroupBy(anon => anon.Group,
                               anon => anon.Str,
                               (key,g) => new
                                 {
                                  Key = key,
                                  Art = g.Count(str => str == "art"),
                                  Economy = g.Count(str => str == "economy"),
                                  Politic = g.Count(str => str == "politic"),
                                  Sport= g.Count(str => str == "sport")
                                 });

foreach(anon in grouping)
{
    //textbox logic OP will have to change to suit
    TextBox1.WriteLine(String.Format("Group: {0}",anon.Key + 1));
    TextBox1.WriteLine(String.Format("Art: {0}",anon.Art));
    TextBox1.WriteLine(String.Format("Economy: {0}",anon.Economy ));
    TextBox1.WriteLine(String.Format("Politic: {0}",anon.Politic ));
    TextBox1.WriteLine(String.Format("Sport: {0}",anon.Sport));
}

答案 2 :(得分:3)

var result = strings.Select((s, i) => new { s, i })
                .GroupBy(x => x.i / 500)
                .Select(x => x.GroupBy(y => y.s)
                                .Select(z => new { 
                                                    Name=z.Key,
                                                    Count=z.Count()
                                                }).ToList())
                .ToList();

答案 3 :(得分:1)

int CountElementsInGroup = 500;
//from 500 to 1000
int NumberGroup = 2;
string[] GroupTypes = new string[4] { "art", "economy", "sport", "politic" };

//Fill example array
string[] arr = new string[2000];
Random rand = new Random();
for (int i = 0; i < arr.Length;i++ )
    arr[i] = GroupTypes[rand.Next(0, 3)];
    var res = (from p in arr.Skip((NumberGroup - 1) * CountElementsInGroup).Take(CountElementsInGroup)
              group p by p into g
              select new GroupCountClass { GroupName = g.Key, GroupCount = g.Count() });

textBox1.Text = "";
foreach (GroupCountClass c in res)
{
   textBox1.Text += String.Format("GroupName:{0} Count:{1};",c.GroupName,c.GroupCount);
}