根据序列的连续性切段的例子:
list2Cut = [1,2,3,6,7,11,13,15,16,17,21]
succesiveGroups = []
succesiveGroup = [list2Cut[0]]
lastValue = list2Cut[0]
for ii in list2Cut[1::]:
if ii-lastValue == 1:
succesiveGroup.append(ii)
else:
succesiveGroups.append(succesiveGroup)
succesiveGroup = [ii]
lastValue = ii
succesiveGroups.append(succesiveGroup)
print(succesiveGroups)
输出结果:
>> [[1, 2, 3], [6, 7], [11], [13], [15, 16, 17], [21]]