### 一、前言

a = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]b = [0, 1, 2, 0, 1, 2, 0, 1, 0, 0, 1, 2, 3, 4, 5]

### 二、实现过程

list1=[1,1,1,0,0,0,1,1,0,1,1,1,1,1,1]result=[0]flag=0for i in range(1,len(list1)):    if list1[i]==list1[i-1]:        flag+=1        else:        flag=0    result.append(flag)print(result)

list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]list2 = []l = 0for i in range(len(list1)):    l = l + 1 if list1[i] == list1[i-1] and i != 0 else 0    list2.append(l)print(list2)

list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]result = [0] * len(list1)result[0] = 0for i in range(1, len(list1)):    if list1[i] != list1[i-1]:        result[i] = 0    else:        result[i] = result[i-1] + 1print(result)

list1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]result = [0] * len(list1)  # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]pre_num = 0for num in range(len(list1)):    # print("list1[num]:", list1[num])    # print("list1[pre_num]: ", list1[pre_num])    print(num, pre_num)    if list1[num] != list1[pre_num]:        pre_num = num    result[num] = num - pre_num    print(result)print(result)

from functools import reducefrom itertools import groupbylist1 = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1]ret = reduce(lambda x, y: x + y, [[i for i in range(len(list(num)))] for item, num in groupby(list1)])print(ret)