Split a List into smaller lists of N size
public static List<List<float[]>> SplitList(List<float[]> locations, int nSize=30)
{
var list = new List<List<float[]>>();
for (int i = 0; i < locations.Count; i += nSize)
{
list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i)));
}
return list;
}
Generic version:
public static IEnumerable<List<T>> SplitList<T>(List<T> locations, int nSize=30)
{
for (int i = 0; i < locations.Count; i += nSize)
{
yield return locations.GetRange(i, Math.Min(nSize, locations.Count - i));
}
}
How to split an n-size list into smaller lists of maximum 2 items?
I believe this is what you're looking for:
n = [1, 2, 3, 4, 5, 6, 7]
[[i, j] for i, j in zip(n[1:], n[2:])]
Output:
[[2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
Divide a list into smaller lists
So, I hardly tried to make it in one line. Here is what I ended up with
import math
nb_classes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
N = 3
lists = [nb_classes[math.ceil(i): math.ceil(i + len(nb_classes) / N)] for i in (len(nb_classes) / N * j for j in range(N))]
print(lists) # [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11]]
But this may be a little bit complicated, you just need to ceil your indexes
Split a python list into other sublists i.e smaller lists
I'd say
chunks = [data[x:x+100] for x in range(0, len(data), 100)]
If you are using python 2.x instead of 3.x, you can be more memory-efficient by using xrange()
, changing the above code to:
chunks = [data[x:x+100] for x in xrange(0, len(data), 100)]
How do I split a list into equally-sized chunks?
Here's a generator that yields evenly-sized chunks:
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74]]
For Python 2, using xrange
instead of range
:
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in xrange(0, len(lst), n):
yield lst[i:i + n]
Below is a list comprehension one-liner. The method above is preferable, though, since using named functions makes code easier to understand. For Python 3:
[lst[i:i + n] for i in range(0, len(lst), n)]
For Python 2:
[lst[i:i + n] for i in xrange(0, len(lst), n)]
Efficient way to divide a list into lists of n size
You'll want to do something that makes use of List.subList(int, int) views rather than copying each sublist. To do this really easily, use Guava's Lists.partition(List, int) method:
List<Foo> foos = ...
for (List<Foo> partition : Lists.partition(foos, n)) {
// do something with partition
}
Note that this, like many things, isn't very efficient with a List
that isn't RandomAccess
(such as a LinkedList
).
Splitting a list into N parts of approximately equal length
This code is broken due to rounding errors. Do not use it!!!
assert len(chunkIt([1,2,3], 10)) == 10 # fails
Here's one that could work:
def chunkIt(seq, num):
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
Testing:
>>> chunkIt(range(10), 3)
[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]
>>> chunkIt(range(11), 3)
[[0, 1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]
>>> chunkIt(range(12), 3)
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
Split list into N sublist with balanced size C#
Your are rounding off, instead of up:
var playersXGroup = (int)Math.Round(players.Count / numberGroupsRound);
should be
var playersXGroup = (int)Math.Ceiling(players.Count / numberGroupsRound);
Related Topics
Access the Current Httpcontext in ASP.NET Core
Execute Insert Command and Return Inserted Id in Sql
How to Replace the *First Instance* of a String in .Net
Append Lines to a File Using a Streamwriter
Use Linq to Xml With Xml Namespaces
Calling Stored Procedure With Return Value
Anyone Know a Good Workaround For the Lack of an Enum Generic Constraint
Setting a Property by Reflection With a String Value
What Is the 'Dynamic' Type in C# 4.0 Used For
How to Get the Connection String from a Database
Is a Linq Statement Faster Than a 'Foreach' Loop
Convert Linq Expression "Obj =≫ Obj.Prop" into "Parent =≫ Parent.Obj.Prop"
Get Public/External Ip Address
Why Does One Often See "Null != Variable" Instead of "Variable != Null" in C#
Problem With Converting Int to String in Linq to Entities
Contains()' Workaround Using Linq to Entities