Skip to content

Instantly share code, notes, and snippets.

@chawza
Created July 18, 2023 04:40
Show Gist options
  • Save chawza/7bf61849f091ab7f4b1591953889d177 to your computer and use it in GitHub Desktop.
Save chawza/7bf61849f091ab7f4b1591953889d177 to your computer and use it in GitHub Desktop.
Batching
def batch(datas, n=10):
idx = 0
while True:
chunk = datas[idx: idx+10]
if len(chunk) == 0:
break
yield chunk
idx += n
datas = [1,2,3,4,5,6,7,2,3,3,3,2,3,3,3,3,3,3,2,3,3,3,3,3,3,3,5,5,5,5,9,56,6,]
res = list(batch(datas, 10))
print(res)
# [[1, 2, 3, 4, 5, 6, 7, 2, 3, 3], [3, 2, 3, 3, 3, 3, 3, 3, 2, 3], [3, 3, 3, 3, 3, 3, 5, 5, 5, 5], [9, 56, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment