gpt4 book ai didi

python - 如何将python列表分成大小相等的 block ?

转载 作者:IT老高 更新时间:2023-10-28 20:35:29 26 4
gpt4 key购买 nike

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'的简单方法?

最佳答案

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

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

关于python - 如何将python列表分成大小相等的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10364391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com