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)]
Answer from Ned Batchelder on Stack OverflowHere'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)]
Something super simple:
def chunks(xs, n):
n = max(1, n)
return (xs[i:i+n] for i in range(0, len(xs), n))
For Python 2, use xrange() instead of range().
How do I split a list of numbers into equal size (of an array?) to pass into an API as an input?
How to Split a Python List or Iterable Into Chunks โ Real Python
Splitting a list in sublists by values
Efficiently divide numpy array into blocks sized Y x Y and apply function to each block
Easy way to represent your matrix in blocks (keeping intermediate steps, but you don't have to):
block_m = 8 # rows in block block_n = 8 # cols in block A = np.arange(256*256).reshape(256,256) # sample data B = A.reshape((-1, block_m, A.shape[1]//block_n, block_n)) C = B.transpose((0,2,1,3))
This yields (in this case) a 32-by-32-by-8-by-8 array where the first two axes refer to the block matrix. i.e. C[0,0] is the 8-by-8 sub-array in the top left of your original array. In most cases, I believe C should be a view on the original array; changing C[0,0,0,0], for instance, changes A[0,0].
You can loop over the first two axes or apply vectorized functions as needed. As an example, here's me averaging the 8-by-8 subarrays:
block_means = np.sum(C, axis=(2,3)) / 64More on reddit.com
I have a file abc.txt:
1234
2678
3345
4987
5765
6864
7479
I want to split the list into equal chunks (into an array?) so that it looks like this:
group 1: '1234','2678','3345'
group 2: '4987','5765','6864'
group 3: '7479'
the goal is to pass those IDs into an API. I can loop through each one of them single-y but one API call can take 30 values at a time, so it would be more efficient to send chunks of 30. (I have that part of the code working!)
my Python version is 3.6 (I know) , so cant use modules like itertools, etc. I need something easy using simple for, while loops if we can.
this is a follow-up to my original post here.
can anyone help or point me to the right direction. thank you!