list_1=['a','a','a','b','c','c','a','a']
list_2=[]
temp=list_1[0]
count = 0
for item in list_1:
if item!=temp:
list_2.append( str(count) + temp)
count=1
temp=item
else:
count+=1
list_2.append( str(count) + temp)
print(list_2)
使用集合去重的属性可操作:
list_1 = ['a', 'a', 'b', 'c', 'c', 'c']
list_2 = []
for i in set(list_1):
list_2.append('%d%s' % (list_1.count(i), i))
print(list_2)