Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 553 Bytes

64.md

File metadata and controls

33 lines (23 loc) · 553 Bytes
@author jackzhenguo
@desc 
@date 2019/4/2

64 列表等分

from math import ceil

def divide(lst, size):
    if size <= 0:
        return [lst]
    return [lst[i * size:(i+1)*size] for i in range(0, ceil(len(lst) / size))]

测试举例:

r = divide([1, 3, 5, 7, 9], 2)
print(r)  # [[1, 3], [5, 7], [9]]

r = divide([1, 3, 5, 7, 9], 0)
print(r)  # [[1, 3, 5, 7, 9]]

r = divide([1, 3, 5, 7, 9], -3)
print(r)  # [[1, 3, 5, 7, 9]]
[上一个例子](63.md) [下一个例子](65.md)