1

I have a list of 20 curves from index 0 to 19 and I want to get 5 sublists of it having indices:

{0,5,10,15},{1,6,11,16},{2,7,12,17},{3,8,13,18},{4,9,14,19}

Is there any method of sublist by starting index and procedure for following elements of a sublist.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • 1
    You can use LINQ methods to achive it. Check out [this](https://stackoverflow.com/questions/20678653/readable-c-sharp-equivalent-of-python-slice-operation) question. – nonForgivingJesus Aug 01 '19 at 10:32

3 Answers3

1

For a list like this:

List<int> list = new List<int>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };

You can use this code:

List<int> subList1 = list.Skip(0).Where((x, i) => i % 5 == 0).ToList();
List<int> subList2 = list.Skip(1).Where((x, i) => i % 5 == 0).ToList();
List<int> subList3 = list.Skip(2).Where((x, i) => i % 5 == 0).ToList();
List<int> subList4 = list.Skip(3).Where((x, i) => i % 5 == 0).ToList();
List<int> subList5 = list.Skip(4).Where((x, i) => i % 5 == 0).ToList();

Or do it in a for loop:

List<int>[] subLists = new List<int>[5];
for (int i = 0; i < subLists.Length; i++)
{
    subLists[i] = list.Skip(i).Where((x, j) => j % 5 == 0).ToList();
}

The Skip(i) is for selecting the first index and the Where((x, j) => j % 5 == 0) is for taking every 5th element by checking if the index modulo 5 is zero.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • @shershahbacha you don't need skip if you use the right modulo. Look at my solution. – Steven Spyrka Aug 01 '19 at 11:12
  • @StevenSpyrka: I upvoted your solution, its clever, shorter and faster than my solution. Still, there is some value in using two operations: skip and take every 5th element, because it makes the intention clearer. – Eliahu Aaron Aug 01 '19 at 11:33
1

This is what you are looking for.

var list = new List<int>();
for(var i = 0; i<=19; i++) list.Add(i);

var list0 = list.Where(x => x % 5 == 0).ToList();
var list1 = list.Where(x => x % 5 == 1).ToList();
var list2 = list.Where(x => x % 5 == 2).ToList();
var list3 = list.Where(x => x % 5 == 3).ToList();
var list4 = list.Where(x => x % 5 == 4).ToList();
Steven Spyrka
  • 678
  • 7
  • 18
0

You can use a loop over the list and take the index modulo 5 to get the index of the sublist and just add the element to it.

Example:

public static void Main(string[] args)
{
    List<int> outerList = new List<int>();
    for (int i = 0; i < 20; i++)
    {
        outerList.Add(i);
    }

    List<List<int>> innerLists = new List<List<int>>();
    for (int i = 0; i < 5; i++)
    {
        innerLists.Add(new List<int>());
    }

    for (int i = 0; i < outerList.Count; i++)
    {
        innerLists[i % 5].Add(outerList[i]);
    }

    Console.WriteLine("outer List:");
    foreach (int i in outerList)
    {
        Console.Write($" { i } ");
    }
    Console.WriteLine();

    Console.WriteLine("inner List:");
    foreach (List<int> l in innerLists)
    {
        foreach (int i in l)
        {
            Console.Write($" { i } ");
        }
        Console.WriteLine();
    }
    Console.WriteLine();

    Console.Read();
}
sticky bit
  • 36,626
  • 12
  • 31
  • 42