from itertools import groupby
def group_number(ls, step=1):
"""对连续数分组,step设置连续数的步长"""
result = []
for _, i in groupby(map(lambda x: [x[0] * step, x[1]], enumerate(ls)), lambda x: x[1]-x[0]):
result.append([j for _, j in i])
return result
# 输入
ls = [0, -1, -2, -1, 0, 2, 2, 3, 4, 3, 3, 5, 5]
# 输出
step = -1
[[0, -1, -2]
, [-1], [0], [2], [2], [3], [4, 3]
, [3], [5], [5]]
step = 0
[[0], [-1], [-2], [-1], [0], [2, 2]
, [3], [4], [3, 3]
, [5, 5]
]
step = 1
[[0], [-1], [-2, -1, 0]
, [2], [2, 3, 4]
, [3], [3], [5], [5]]
step = 2
[[0], [-1], [-2], [-1], [0, 2]
, [2], [3], [4], [3], [3, 5]
, [5]]