Grouping Python tuple list

前端 未结 7 1606
日久生厌
日久生厌 2020-11-30 07:47

I have a list of (label, count) tuples like this:

[(\'grape\', 100), (\'grape\', 3), (\'apple\', 15), (\'apple\', 10), (\'apple\', 4), (\'banana\', 3)]
         


        
7条回答
  •  一整个雨季
    2020-11-30 08:22

    my version without itertools
    [(k, sum([y for (x,y) in l if x == k])) for k in dict(l).keys()]

提交回复
热议问题