To sum up values of same items in a list of tuples while they are string

后端 未结 5 734
一整个雨季
一整个雨季 2021-01-23 01:56

If I have list of tuples like this:

my_list = [(\'books\', \'$5\'), (\'books\', \'$10\'), (\'ink\', \'$20\'), (\'paper\', \'$15\'), (\'paper\', \'$20\'), (\'pap         


        
5条回答
  •  难免孤独
    2021-01-23 02:58

    Just for fun, a one-liner:

    [(k,'$'+str(sum(int(e[1][1:]) for e in my_list if e[0]==k))) for k in set(map(lambda x:x[0], my_list))]
    

    Don't actually do this.

提交回复
热议问题