📜  Python 使用 itertools 方法将列表拆分成块 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:18.157000             🧑  作者: Mango

代码示例1
# Split a Python List into Chunks using itertools

from itertools import zip_longest

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size=2
result=list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))
print(result)

chunked_list = [list(item) for item in list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))]
print(chunked_list)