def get_list_span(l: list): """ For list ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'd', 'd', 'd'], its span list is [3, 0, 0, 1, 2, 0, 4, 0, 0, 0] . :param l: :return: """ span_list = [] span = 1 for i in range(len(l)): if i == 0: # 第一项span设为1 span_list.append(1) else: if l[i] == l[i-span]: # 与之前项相等,之前项的span值加1,本项span设为0,span值加1 span_list[i-span] += 1 span_list.append(0) span += 1 else: # 与之前项不等,本项span设为1,span值恢复为1 span_list.append(1) span = 1 return span_list