PHP
·
发表于 5年以前
·
阅读量:8398
>>> from math import ceil
>>> def divide_iter(lst, n):
if n <= 0:
yield lst
return
i, div = 0, ceil(len(lst) / n)
while i < n:
yield lst[i * div: (i + 1) * div]
i += 1
>>> for group in divide_iter([1,2,3,4,5],2):
print(group)
[1, 2, 3]
[4, 5]