Python - sum values in dictionary

后端 未结 3 894
逝去的感伤
逝去的感伤 2020-11-29 20:40

I have got pretty simple list:

example_list = [
    {\'points\': 400, \'gold\': 2480},
    {\'points\': 100, \'gold\': 610},
    {\'points\': 100, \'gold\':          


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 20:57

    If you prefer map, this works too:

     import operator
     total_gold = sum(map(operator.itemgetter('gold'),example_list))
    

    But I think the generator posted by g.d.d.c is significantly better. This answer is really just to point out the existence of operator.itemgetter.

提交回复
热议问题