How to merge similar items in a list

后端 未结 6 647
花落未央
花落未央 2021-01-19 02:50

I haven\'t found anything relevant on Google, so I\'m hoping to find some help here :)

I\'ve got a Python list as follows:

[[\'hoose\', 200], [\"Ba         


        
6条回答
  •  长情又很酷
    2021-01-19 03:00

    Blueprint:

    result = dict()
    for item in [[['hoose', 5], 200], [['House', 5], 200], [["Bananaphone", 5], 10], ...]:
    
       key = item[0] # ('hoose', 5)
       value = item[1] # 200
    
       if key in result:
           result[key] = 0
       result[key] += value
    

    It might be necessary to adjust the code for unpacking the inner list items.

提交回复
热议问题