I am wondering how to separate all elements in a list into several groups with the same size of length.
For example, there is a list containing 12 elements: A = [1,2,3,4,5,6,7,8,9,10,11,12]
.
Then, 4 adjacent elements in A
are put into a sublist.
Finally, the results would be like: [1,2,3,4], [5,6,7,8],[9,10,11,12]
Thanks in advance!
CodePudding user response:
You could try this handy one by using more_itertools:
It's to group elements from iterable into fixed-length groups of length n (in this case is 4)
>>> from more_itertools import grouper
>>> list(grouper(lst, 3))
[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]
>>> list(grouper(lst, 4))
[(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]
# Or strictly list of groups:
>>> groups = [list(x) for x in grouper(lst, 4) ]
>>> groups
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Alternatively, you can use pure Python (w/o 3rd party lib):
n = 4
gps = [list(lst[i*n:i*n n]) for i in range(n-1)]
CodePudding user response:
Based on Daniel Hao's answer, here is a generalization for n groups:
from more_itertools import grouper
# could be any list
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
def divide_list(input_list, n):
# check for adequate group size
if n > len(input_list):
print('group size bigger than original list.')
return None
return [list(x) for x in grouper(input_list, n)]
Testing:
test = divide_list(A, 5)
print(test)
# output:
# [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, None, None]]
CodePudding user response:
A division with rest and slice approach.
A = [1,2,3,4,5,6,7,8,9,10,11,12]
blk_size = 5
A_chunked = [A[blk_size*i:blk_size*(i 1)] for i in range(len(A)//blk_size)]
# if not possible to group all elements chunk of the same size
if r:=len(A) % blk_size:
A_chunked.append(A[-r:])
print(A_chunked)
#[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12]]
It the chunks need to be all of the same size then either truncate the iteration (hence the conditional part is not required) or compensate the rest with some default values, here None
,
if r:=len(A) % blk_size:
A_chunked.append(A[-r:] [None]*(blk_size - r))