Created
July 18, 2023 04:40
-
-
Save chawza/7bf61849f091ab7f4b1591953889d177 to your computer and use it in GitHub Desktop.
Batching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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