0

I have a list of dynamic of size 85160 (but it can change). I'd like to split it into 3 equal sized lists.

I was thinking of getting the size of the list. So that'd just be:

int count = rawData.count;

Then I could divide that by 3; but i'm not sure how i'd stick the first set into one list, then the next etc.

Best way of doing it?

Chris Gregori
  • 65
  • 1
  • 1
  • 8

6 Answers6

1

This should work.

var items = rawData.Count / 3;
var first = rawData.Take(items).ToList();
var second = rawData.Skip(items).Take(items).ToList();
var third = rawDate.Skip(2 * items).ToList();
DanielB
  • 19,910
  • 2
  • 44
  • 50
1

Try this code if you don't care about ordering of items in collection.

static IEnumerable<IEnumerable<int>> Split(IList<int> source, int count)
    {
        return source
            .Select((value, index) => new { Index = index % count, Value = value })
            .GroupBy(pair => pair.Index)
            .Select(grp => grp.Select(g => g.Value));
    }

Example of usage

static void Main()
{
   var arrays = Split(new[]{1,2,3,4,5,6,7,8,9,0}, 3);

   foreach(var array in arrays)
   {
        foreach(var item in array)
            Console.WriteLine(item);
        Console.WriteLine("---------------");
   }
}

wil give you

1 4 7 0


2 5 8


3 6 9

user854301
  • 5,383
  • 3
  • 28
  • 37
0

May be, List.GetRange(int index, int count) will help?

Lunik
  • 392
  • 1
  • 6
  • You're better off writing answers conclusively: i.e. don't answer with a question, even if it contains information that could be an answer. –  Aug 02 '12 at 10:02
0
var lists = new List<List<YourObject>>();
int chunkCount = 3;
int chunk = rawData / chunkCount;
for (int i = 0; i < chunkCount; i++)
{
    var data = rawData.Skip(i * chunk);

    if (i < chunkCount - 1)
        data = data.Take(chunk);

    lists.Add(data.ToList());
}

When i == chunkCount - 1 (last iteration) I didn't use Take to make sure I take everything till the end.

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
0

Well, you could do it generally for all IEnumerables with an extension like this.

public static IEnumerable<IEnumerable<T>> Chop<T>(
    this IEnumerable<T> source,
    int chopCount)
{
    var chops = new IList<T>[chopCount];

    for (i = 0; i < chops.Length; i++)
    {
        chops[i] = new List<T>();
    }

    var nextChop = 0;
    foreach (T item in source)
    {
        chop[nextChop].Add(item);
        nextChop = nextChop == chopCount - 1 ? 0 : nextChop + 1;
    }

    for (i = 0; i < chops.Length; i++)
    {
        yield return chops[i];
    }   
}

which you could use like this

var chops = rawData.Chop(3);
Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

Here's a solution that uses an extension to IEnumerable which partitions a sequence into a number of sub-sequences:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 85160;
            var someNumbers = Enumerable.Range(0, count).ToList();
            var lists = PartitionToEqualSizedLists(someNumbers, 3);

            foreach (var list in lists)
            {
                Console.WriteLine("List length = " + list.Count);
            }

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }

        public static List<List<T>> PartitionToEqualSizedLists<T>(List<T> input, int numPartitions)
        {
            int blockSize = (input.Count + numPartitions - 1)/numPartitions;
            return input.Partition(blockSize).Select(partition => partition.ToList()).ToList();
        }
    }

    public static class EnumerableExt
    {
        public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> input, int blockSize)
        {
            var enumerator = input.GetEnumerator();

            while (enumerator.MoveNext())
            {
                yield return nextPartition(enumerator, blockSize);
            }
        }

        private static IEnumerable<T> nextPartition<T>(IEnumerator<T> enumerator, int blockSize)
        {
            do
            {
                yield return enumerator.Current;
            }
            while (--blockSize > 0 && enumerator.MoveNext());
        }
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276