Split a List into smaller lists of N size

前端 未结 17 1647
后悔当初
后悔当初 2020-11-22 16:55

I am attempting to split a list into a series of smaller lists.

My Problem: My function to split lists doesn\'t split them into lists of the correct

相关标签:
17条回答
  • 2020-11-22 17:35

    Serj-Tm solution is fine, also this is the generic version as extension method for lists (put it into a static class):

    public static List<List<T>> Split<T>(this List<T> items, int sliceSize = 30)
    {
        List<List<T>> list = new List<List<T>>();
        for (int i = 0; i < items.Count; i += sliceSize)
            list.Add(items.GetRange(i, Math.Min(sliceSize, items.Count - i)));
        return list;
    } 
    
    0 讨论(0)
  • 2020-11-22 17:38

    While plenty of the answers above do the job, they all fail horribly on a never ending sequence (or a really long sequence). The following is a completely on-line implementation which guarantees best time and memory complexity possible. We only iterate the source enumerable exactly once and use yield return for lazy evaluation. The consumer could throw away the list on each iteration making the memory footprint equal to that of the list w/ batchSize number of elements.

    public static IEnumerable<List<T>> BatchBy<T>(this IEnumerable<T> enumerable, int batchSize)
    {
        using (var enumerator = enumerable.GetEnumerator())
        {
            List<T> list = null;
            while (enumerator.MoveNext())
            {
                if (list == null)
                {
                    list = new List<T> {enumerator.Current};
                }
                else if (list.Count < batchSize)
                {
                    list.Add(enumerator.Current);
                }
                else
                {
                    yield return list;
                    list = new List<T> {enumerator.Current};
                }
            }
    
            if (list?.Count > 0)
            {
                yield return list;
            }
        }
    }
    

    EDIT: Just now realizing the OP asks about breaking a List<T> into smaller List<T>, so my comments regarding infinite enumerables aren't applicable to the OP, but may help others who end up here. These comments were in response to other posted solutions that do use IEnumerable<T> as an input to their function, yet enumerate the source enumerable multiple times.

    0 讨论(0)
  • 2020-11-22 17:43

    Library MoreLinq have method called Batch

    List<int> ids = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; // 10 elements
    int counter = 1;
    foreach(var batch in ids.Batch(2))
    {
        foreach(var eachId in batch)
        {
            Console.WriteLine("Batch: {0}, Id: {1}", counter, eachId);
        }
        counter++;
    }
    

    Result is

    Batch: 1, Id: 1
    Batch: 1, Id: 2
    Batch: 2, Id: 3
    Batch: 2, Id: 4
    Batch: 3, Id: 5
    Batch: 3, Id: 6
    Batch: 4, Id: 7
    Batch: 4, Id: 8
    Batch: 5, Id: 9
    Batch: 5, Id: 0
    

    ids are splitted into 5 chunks with 2 elements.

    0 讨论(0)
  • 2020-11-22 17:44

    I would suggest to use this extension method to chunk the source list to the sub-lists by specified chunk size:

    /// <summary>
    /// Helper methods for the lists.
    /// </summary>
    public static class ListExtensions
    {
        public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize) 
        {
            return source
                .Select((x, i) => new { Index = i, Value = x })
                .GroupBy(x => x.Index / chunkSize)
                .Select(x => x.Select(v => v.Value).ToList())
                .ToList();
        }
    }
    

    For example, if you chunk the list of 18 items by 5 items per chunk, it gives you the list of 4 sub-lists with the following items inside: 5-5-5-3.

    0 讨论(0)
  • 2020-11-22 17:44

    how about:

    while(locations.Any())
    {    
        list.Add(locations.Take(nSize).ToList());
        locations= locations.Skip(nSize).ToList();
    }
    
    0 讨论(0)
  • 2020-11-22 17:44

    I have a generic method that would take any types include float, and it's been unit-tested, hope it helps:

        /// <summary>
        /// Breaks the list into groups with each group containing no more than the specified group size
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="values">The values.</param>
        /// <param name="groupSize">Size of the group.</param>
        /// <returns></returns>
        public static List<List<T>> SplitList<T>(IEnumerable<T> values, int groupSize, int? maxCount = null)
        {
            List<List<T>> result = new List<List<T>>();
            // Quick and special scenario
            if (values.Count() <= groupSize)
            {
                result.Add(values.ToList());
            }
            else
            {
                List<T> valueList = values.ToList();
                int startIndex = 0;
                int count = valueList.Count;
                int elementCount = 0;
    
                while (startIndex < count && (!maxCount.HasValue || (maxCount.HasValue && startIndex < maxCount)))
                {
                    elementCount = (startIndex + groupSize > count) ? count - startIndex : groupSize;
                    result.Add(valueList.GetRange(startIndex, elementCount));
                    startIndex += elementCount;
                }
            }
    
    
            return result;
        }
    
    0 讨论(0)
提交回复
热议问题