在工作或参加面试过程中,经常遇到将一个列表,按指定长度分割成多个列表的问题。详细实现过程,看如下具体实例:
from math import ceil
def chunk(lst, size):
return list(
map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(lst) / size)))))
函数编写完后,调用函数看看效果吧。
chunk([1,2,3,4,5],2)
最后输出结果:
[[1,2],[3,4],5]
每天学点Python小知识或编程小技巧,让你的编码水平与日俱增。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有