给出类似的东西:
l = [0,0,0,1,1,1,2,2,2,2,3,3]
我想:
[[0,1,2], [3,4,5], [6,7,8,9], [10,11]]
现在,我正在做:
[[elem[0] for elem in list(g)] for k, g in itertools.groupby(
enumerate(l), lambda x: x[1])]
有更好,更快,更简单的方法吗?
谢谢!
答案 0 :(得分:1)
这看起来很简单。
result = []
last = None
for i, this in enumerate(a):
if this != last:
sub = []
result.append(sub)
last = this
sub.append(i)