Summing values in nested list when item changes

后端 未结 5 1169
眼角桃花
眼角桃花 2021-01-23 07:45

I have some problem which has stumped my beginner-knowledge of python and I hope someone out there can point me in the correct direction.

I generated a nested list, each

5条回答
  •  生来不讨喜
    2021-01-23 08:09

    You can try using defaultdict ...

    from collections import defaultdict
    
    dat = [[1, 0],[1, 2],[2, 9],[3, 0],[3, 8],[3, 1]]
    
    d = defaultdict(int)
    for k,v in dat: d[k] += v
    

提交回复
热议问题