<bdo id='FcVgk'></bdo><ul id='FcVgk'></ul>

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

    <tfoot id='FcVgk'></tfoot>
  • <legend id='FcVgk'><style id='FcVgk'><dir id='FcVgk'><q id='FcVgk'></q></dir></style></legend>

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

        如何减少python中的元组列表

        时间:2023-09-12
      1. <i id='L6vwF'><tr id='L6vwF'><dt id='L6vwF'><q id='L6vwF'><span id='L6vwF'><b id='L6vwF'><form id='L6vwF'><ins id='L6vwF'></ins><ul id='L6vwF'></ul><sub id='L6vwF'></sub></form><legend id='L6vwF'></legend><bdo id='L6vwF'><pre id='L6vwF'><center id='L6vwF'></center></pre></bdo></b><th id='L6vwF'></th></span></q></dt></tr></i><div id='L6vwF'><tfoot id='L6vwF'></tfoot><dl id='L6vwF'><fieldset id='L6vwF'></fieldset></dl></div>
          <bdo id='L6vwF'></bdo><ul id='L6vwF'></ul>

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

          • <legend id='L6vwF'><style id='L6vwF'><dir id='L6vwF'><q id='L6vwF'></q></dir></style></legend>

          • <tfoot id='L6vwF'></tfoot>

              <tbody id='L6vwF'></tbody>

                  本文介绍了如何减少python中的元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个数组,我想计算数组中每个项目的出现次数.

                  I have an array and I want to count the occurrence of each item in the array.

                  我已经设法使用 map 函数来生成一个元组列表.

                  I have managed to use a map function to produce a list of tuples.

                  def mapper(a):
                      return (a, 1)
                  
                  r = list(map(lambda a: mapper(a), arr));
                  
                  //output example: 
                  //(11817685, 1), (2014036792, 1), (2014047115, 1), (11817685, 1)
                  

                  我希望 reduce 函数可以帮助我按每个元组中的第一个数字 (id) 对计数进行分组.例如:

                  I'm expecting the reduce function can help me to group counts by the first number (id) in each tuple. For example:

                  (11817685, 2), (2014036792, 1), (2014047115, 1)
                  

                  我试过了

                  cnt = reduce(lambda a, b: a + b, r);
                  

                  还有其他一些方法,但它们都不起作用.

                  and some other ways but they all don't do the trick.

                  注意感谢所有关于解决问题的其他方法的建议,但我只是在学习 Python 以及如何在这里实现 map-reduce,并且我已经简化了我的实际业务问题以使其易于理解,所以请告诉我一个正确的方法来做 map-reduce.

                  NOTE Thanks for all the advice on other ways to solve the problems, but I'm just learning Python and how to implement a map-reduce here, and I have simplified my real business problem a lot to make it easy to understand, so please kindly show me a correct way of doing map-reduce.

                  推荐答案

                  你可以使用Counter:

                  from collections import Counter
                  arr = [11817685, 2014036792, 2014047115, 11817685]
                  counter = Counter(arr)
                  print zip(counter.keys(), counter.values())
                  

                  正如@ShadowRanger 所指出的, Counteritems() 方法:

                  As pointed by @ShadowRanger Counter has items() method:

                  from collections import Counter
                  arr = [11817685, 2014036792, 2014047115, 11817685]
                  print Counter(arr).items()
                  

                  这篇关于如何减少python中的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从python中的字符串中提取英文单词 下一篇:mapreduce 难以理解

                  相关文章

                  最新文章

                    <bdo id='zse3x'></bdo><ul id='zse3x'></ul>

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

                    <legend id='zse3x'><style id='zse3x'><dir id='zse3x'><q id='zse3x'></q></dir></style></legend>
                  1. <tfoot id='zse3x'></tfoot>

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