mylist = ["a","a","b","c","c","c","d","d","d","d","e","f"] length = len(mylist) print(length) i = 0 while i < length: times = 1 passwordstr = mylist[i] while i < length-1: if mylist[i] == mylist[i+1]: times+=1 i += 1 else: break print(times,passwordstr) i += 1
输出结果
12 2 a 1 b 3 c 4 d 1 e 1 f