Не усложняя, вы можете просто решить это примерно так:
def chunk_lists_(data_):
consecutive_list = []
for chunks in range(len(data_)):
try:
#check consecutiveness
if data_[chunks + 1] - data_[chunks] == 1:
#check if it's already in list
if data_[chunks] not in consecutive_list:
consecutive_list.append(data_[chunks])
#add last one too
consecutive_list.append(data_[chunks + 1])
else:
#yield here and empty list
yield consecutive_list
consecutive_list = []
except Exception:
pass
yield consecutive_list
Тест:
#Stephen's list
print(list(chunk_lists_(list(range(0, 11)) +
list(range(80, 91)) +
list(range(160, 171)))))
выход: * +1010 *
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170]]