我有一些代码可以做到这一点。我不确定这是否是处理很长列表的最佳方法,但它应该适用于您发布的列表。
ps。没有 numpy :)
def unique(list1):
# intilize a null list
unique_list = []
# traverse for all elements
for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
# print list
return unique_list
My_list = [1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1]
#Create an array with the unique values from your list
unique_list = unique(My_list)
#Create a list with size = amount of unique values
zeros = [0 for _ in range(len(unique_list))]
count = 1
for i in range(0,len(My_list)-1):
#Is the following consecutive value equal to the current value?
if (My_list[i] == My_list[i+1]) and (i != len(My_list)-2):
#count how many consecutive repetitions you have
count = count + 1
else:
idx = unique_list.index(My_list[i])
#If it's the highest amount of repetitions, save it
if zeros[idx] < count:
zeros[idx] = count
count = 1
for i in range(0, len(unique_list)):
print("Highest consecutive "+ str(unique_list[i]) + "'s are: " + str(zeros[i]))