import pandas as pd
import numpy as np
l = [0, 11, 23, 33, 0, 0, 0, 76, 0, 41, 68]
df = pd.DataFrame({'A': l})
df['block'] = (df['A'] == 0).astype(int).cumsum() # 对等于0的进行累加计算。
df = df.reset_index()
df = df[df.A != 0] # 删除掉为0 的 元素
print(l)
print df.groupby(['block'])['index'].apply(np.array).tolist()
结果:
[0, 11, 23, 33, 0, 0, 0, 76, 0, 41, 68]
[array([1, 2, 3]), array([7]), array([ 9, 10])]