您可以用来
itertools.zip_longest对列表中的顺序元素对启用迭代,并
enumerate跟踪序列未增加的索引值,以便将相应的切片附加到输出列表中。
from itertools import zip_longestnums = [1, 4, 1, 2, 4, 3, 5, 4, 0]results = []start = 0for i, (a, b) in enumerate(zip_longest(nums, nums[1:])): if b is None or b <= a: results.append(nums[start:i+1]) start = i + 1print(results)# [[1, 4], [1, 2, 4], [3, 5], [4], [0]]



