如何将python列表拆分成相等大小的块? [英] How to split python list into chunks of equal size?

查看:60
本文介绍了如何将python列表拆分成相等大小的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何将列表平均划分大小的Python块?
python:转换"5,4,2,4,1,0"变成[[5,4],[2,4],[1,0]]

Possible Duplicate:
How do you split a list into evenly sized chunks in Python?
python: convert “5,4,2,4,1,0” into [[5, 4], [2, 4], [1, 0]]

[1,2,3,4,5,6,7,8,9]

->

[[1,2,3],[4,5,6],[7,8,9]]

有没有简单的方法,而无需显式的"for"吗?

Are there simple way to do it, without explicit 'for'?

推荐答案

>>> x = [1,2,3,4,5,6,7,8,9]
>>> zip(*[iter(x)]*3)
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

zip(*[iter(s)]*n)如何在Python中工作?

How does zip(*[iter(s)]*n) work in Python?

这篇关于如何将python列表拆分成相等大小的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆