<legend id='Sb0gP'><style id='Sb0gP'><dir id='Sb0gP'><q id='Sb0gP'></q></dir></style></legend>

  1. <tfoot id='Sb0gP'></tfoot>
  2. <small id='Sb0gP'></small><noframes id='Sb0gP'>

    <i id='Sb0gP'><tr id='Sb0gP'><dt id='Sb0gP'><q id='Sb0gP'><span id='Sb0gP'><b id='Sb0gP'><form id='Sb0gP'><ins id='Sb0gP'></ins><ul id='Sb0gP'></ul><sub id='Sb0gP'></sub></form><legend id='Sb0gP'></legend><bdo id='Sb0gP'><pre id='Sb0gP'><center id='Sb0gP'></center></pre></bdo></b><th id='Sb0gP'></th></span></q></dt></tr></i><div id='Sb0gP'><tfoot id='Sb0gP'></tfoot><dl id='Sb0gP'><fieldset id='Sb0gP'></fieldset></dl></div>

      • <bdo id='Sb0gP'></bdo><ul id='Sb0gP'></ul>
    1. 在元组列表中按字母对数字求和

      Sum numbers by letter in list of tuples(在元组列表中按字母对数字求和)
          <bdo id='TcYoB'></bdo><ul id='TcYoB'></ul>
            <tbody id='TcYoB'></tbody>
          1. <tfoot id='TcYoB'></tfoot>

          2. <legend id='TcYoB'><style id='TcYoB'><dir id='TcYoB'><q id='TcYoB'></q></dir></style></legend>

            • <small id='TcYoB'></small><noframes id='TcYoB'>

              <i id='TcYoB'><tr id='TcYoB'><dt id='TcYoB'><q id='TcYoB'><span id='TcYoB'><b id='TcYoB'><form id='TcYoB'><ins id='TcYoB'></ins><ul id='TcYoB'></ul><sub id='TcYoB'></sub></form><legend id='TcYoB'></legend><bdo id='TcYoB'><pre id='TcYoB'><center id='TcYoB'></center></pre></bdo></b><th id='TcYoB'></th></span></q></dt></tr></i><div id='TcYoB'><tfoot id='TcYoB'></tfoot><dl id='TcYoB'><fieldset id='TcYoB'></fieldset></dl></div>
                本文介绍了在元组列表中按字母对数字求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个元组列表:

                [ ('A',100), ('B',50), ('A',50), ('B',20), ('C',10) ]
                

                我试图总结所有具有相同字母的数字.IE.我要输出

                I am trying to sum up all numbers that have the same letter. I.e. I want to output

                [('A', 150), ('B', 70), ('C',10)] 
                

                我尝试使用 set 来获取唯一值,但是当我尝试将第一个元素与 set 进行比较时,我得到了

                I have tried using set to get the unique values but then when I try and compare the first elements to the set I get

                TypeError: unsupported operand type(s) for +: 'int' and 'str'
                

                有什么快速的方法可以按字母匹配数字吗?

                Any quick solutions to match the numbers by letter?

                推荐答案

                这是一个(半?)-liner:按字母分组(您需要先排序),然后取第二个的总和元组的条目.

                Here is a one(and a half?)-liner: group by letter (for which you need to sort before), then take the sum of the second entries of your tuples.

                from itertools import groupby
                from operator import itemgetter
                
                data = [('A', 100), ('B', 50), ('A', 50), ('B', 20), ('C', 10)]
                res = [(k, sum(map(itemgetter(1), g)))
                       for k, g in groupby(sorted(data, key=itemgetter(0)), key=itemgetter(0))]
                print(res)
                // => [('A', 150), ('B', 70), ('C', 10)]
                

                上面是O(n log n) —排序是最昂贵的操作.如果您的输入列表确实很大,那么以下 O(n) 方法可能会更好地为您服务:

                The above is O(n log n) — sorting is the most expensive operation. If your input list is truly large, you might be better served by the following O(n) approach:

                from collections import defaultdict
                
                data = [('A', 100), ('B', 50), ('A', 50), ('B', 20), ('C', 10)]
                
                d = defaultdict(int)
                for letter, value in data:
                    d[letter] += value
                res = list(d.items())
                print(res)
                // => [('B', 70), ('C', 10), ('A', 150)]
                

                这篇关于在元组列表中按字母对数字求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

              • <legend id='hAWWt'><style id='hAWWt'><dir id='hAWWt'><q id='hAWWt'></q></dir></style></legend>
                      <i id='hAWWt'><tr id='hAWWt'><dt id='hAWWt'><q id='hAWWt'><span id='hAWWt'><b id='hAWWt'><form id='hAWWt'><ins id='hAWWt'></ins><ul id='hAWWt'></ul><sub id='hAWWt'></sub></form><legend id='hAWWt'></legend><bdo id='hAWWt'><pre id='hAWWt'><center id='hAWWt'></center></pre></bdo></b><th id='hAWWt'></th></span></q></dt></tr></i><div id='hAWWt'><tfoot id='hAWWt'></tfoot><dl id='hAWWt'><fieldset id='hAWWt'></fieldset></dl></div>
                          <bdo id='hAWWt'></bdo><ul id='hAWWt'></ul>

                            <tbody id='hAWWt'></tbody>

                          <tfoot id='hAWWt'></tfoot>

                          <small id='hAWWt'></small><noframes id='hAWWt'>