1. <small id='tmVAK'></small><noframes id='tmVAK'>

      <tfoot id='tmVAK'></tfoot><legend id='tmVAK'><style id='tmVAK'><dir id='tmVAK'><q id='tmVAK'></q></dir></style></legend>

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

        如何按常见项目对元组进行分组并找到每组的平均值

        How to group tuples by common items and find average per each group(如何按常见项目对元组进行分组并找到每组的平均值)
        <tfoot id='qUczX'></tfoot>

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

              <legend id='qUczX'><style id='qUczX'><dir id='qUczX'><q id='qUczX'></q></dir></style></legend>
                • <bdo id='qUczX'></bdo><ul id='qUczX'></ul>

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

                  本文介绍了如何按常见项目对元组进行分组并找到每组的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个名为 data 的元组列表:

                  I have a list of tuples named data:

                  data = [('A', 2), 
                          ('B', 2), ('B', 4), ('B', 6), ('B', 8), ('B', 6), ('B', 4), ('B', 3),
                          ('C', 10), ('C', 10), ('C', 10),
                          ('D', 12),
                          ('E', 12),
                          ('F', 10), ('F', 8), ('F', 6)]
                  average = []
                  

                  我想要每个相同字母的平均值:

                  I would like an average of the values for each same letter:

                  预期输出:

                  average = [('A', 2), ('B', 5), ('C', 10), ('D', 12), ('E', 12), ('F', 8)]
                  

                  推荐答案

                  试试 groupby

                  from itertools import groupby
                  data_ = [(n,[i[1] for i in g]) for n,g in groupby(data, key = lambda x:x[0])]   
                  result = [(i,float(sum(j))/float(len(j))) for i,j in data_]
                  

                  结果

                  [('A', 2.0),
                   ('B', 4.714285714285714),
                   ('C', 10.0),
                   ('D', 12.0),
                   ('E', 12.0),
                   ('F', 8.0)]
                  

                  这篇关于如何按常见项目对元组进行分组并找到每组的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                    <tbody id='AqWL0'></tbody>
                • <small id='AqWL0'></small><noframes id='AqWL0'>

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

                    • <bdo id='AqWL0'></bdo><ul id='AqWL0'></ul>

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