列表按相同元素分割
如将列表[0,0,0,1,1,2,3,3,3,2,3,3,0,0]分割成[0,0,0],[1,1],[2],[3,3,3],[2],[3,3],[0,0]
思路:取出相同元素的索引,根据索引再去对列表进行切片
def split_by_item(num_list): temp = [] split_list=[] for i in range(len(num_list)): if i < len(num_list)-1: if num_list[i] != num_list[i+1]: temp.append(i+1) temp.append(len(num_list)) for j in range(len(temp)): if j == 0: split_list.append(num_list[:temp[j]]) else: split_list.append(num_list[temp[j-1]:temp[j]]) return split_list