Python列表转换怎么写比较高效,python列表怎么写,a = [1, 1, 2


a = [1, 1, 2, 2, 2, 3]
元素已排好序
希望变成:
b = [2, 4, 5]
即按照出现次数累加
怎么写比较简洁高效?

>>> valdict = dict((k, len(list(g))) for k, g in groupby(sorted(a)))>>>>>> for key, val in valdict.items(): print key, ":", val...1 : 22 : 33 : 1
python
import collectionscounter = collections.Counter([1, 1, 2, 2, 2, 3])print counter>>> Counter({2: 3, 1: 2, 3: 1})

编橙之家文章,

评论关闭