我有一个字符串的Python列表,
输入:
li = ['aaa','bbb','aaa','abb','abb','bbb','bbb','bbb','aaa','aaa']
如何生成另一个列表,计算列表中任何字符串的连续重复次数?对于上面的列表,返回列表类似于:
预期产出:
li_count = [['aaa',1],['bbb',1]['abb',2],['bbb',3],['aaa',2]]
答案 0 :(得分:7)
uuid().replace(/-/g, '')
谢谢@ user3483203的改进:
from itertools import groupby
li = ['aaa','bbb','aaa','abb','abb','bbb','bbb','bbb','aaa','aaa']
a = [[i, sum(1 for i in group)] for i, group in groupby(li)]
print(a)
[['aaa', 1], ['bbb', 1], ['aaa', 1], ['abb', 2], ['bbb', 3], ['aaa', 2]]