none
Split List<double> into sub lists of List<double> of size n

    Question

  • Hi all,

     

    I've been playing around for a little bit trying to find the best way of taking a List<double> of variable size and splitting it in to sub-lists of  size n. So maybe a method stub like

     

    public List<List<double>> splitData(int width,List<double> data)
    {
    <br/>
    }<br/>
    
    

    The problem is i cant get my head around how to iterate through the parameter "data" properly when the data.count is not exactly divisible by the parameter "width". I have been trying for loops but i think i need to switch to do whiles.

     

    I'm not massively familiar with LINQ but could anyone advise if LINQ would provide an alternative way of doing it?

     

    Any help much appreciated

     

    John

    Friday, August 27, 2010 9:21 PM

Answers

  • Something like this might work:

        private void Form1_Load(object sender, EventArgs e)
        {
          List<double> dd = new List<double>() {1,2,3,4,5,6,7,8,9,10,.1,.2,.3,.4,.5,.6,.7,.8,.9};
          List<List<double>> splitdd=splitData(4, dd);
        }
    
        public List<List<double>> splitData(int width, List<double> dd)
        {
          List<List<double>> dds = new List<List<double>>();
    
          // Determine how many lists are required
          int numberOfLists = (dd.Count / width) + 1;
    
          for (int i = 0; i < numberOfLists; i++)
          {
            List<double> newdd = new List<double>();
            newdd = dd.Skip(i*width).Take(width).ToList();
            dds.Add(newdd);
          }
    
          return dds;
        }
    
    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by McTash Friday, August 27, 2010 10:16 PM
    Friday, August 27, 2010 10:06 PM

All replies

  • Something like this might work:

        private void Form1_Load(object sender, EventArgs e)
        {
          List<double> dd = new List<double>() {1,2,3,4,5,6,7,8,9,10,.1,.2,.3,.4,.5,.6,.7,.8,.9};
          List<List<double>> splitdd=splitData(4, dd);
        }
    
        public List<List<double>> splitData(int width, List<double> dd)
        {
          List<List<double>> dds = new List<List<double>>();
    
          // Determine how many lists are required
          int numberOfLists = (dd.Count / width) + 1;
    
          for (int i = 0; i < numberOfLists; i++)
          {
            List<double> newdd = new List<double>();
            newdd = dd.Skip(i*width).Take(width).ToList();
            dds.Add(newdd);
          }
    
          return dds;
        }
    
    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by McTash Friday, August 27, 2010 10:16 PM
    Friday, August 27, 2010 10:06 PM
  • Thanks very much that looks just the ticket!

     

     

    EDIT: just tried it, works a charm thanks again.

    john

    Friday, August 27, 2010 10:15 PM
  • The following line won't work if dd.Count is a multiple of width.

    int numberOfLists = (dd.Count / width) + 1;

    I modified as follows and verified it works correctly:

    int numberOfLists = (dd.Count / width) + (dd.Count % width > 0 ? 1 : 0);
    Friday, July 22, 2011 4:42 AM