python将列表中连续重复的元素个数输出

python将列表中连续重复的元素个数输出

问题描述:

怎么将列表[3,2,2,2,6,6,3,3,7,9,9,9,9]变成[(3,1),(2,3),(6,2),(3,2),(7,1),(9,4)]的形式

from collections import Counter

cs_list = [3, 2, 2, 2, 6, 6, 3, 3, 7, 9, 9, 9, 9]
a = Counter(cs_list)
print(list(a.items()))