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

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

      <tfoot id='A5Hpj'></tfoot>

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

        计算python字典中某个值的出现次数?

        count the number of occurrences of a certain value in a dictionary in python?(计算python字典中某个值的出现次数?)
        <i id='Cv95x'><tr id='Cv95x'><dt id='Cv95x'><q id='Cv95x'><span id='Cv95x'><b id='Cv95x'><form id='Cv95x'><ins id='Cv95x'></ins><ul id='Cv95x'></ul><sub id='Cv95x'></sub></form><legend id='Cv95x'></legend><bdo id='Cv95x'><pre id='Cv95x'><center id='Cv95x'></center></pre></bdo></b><th id='Cv95x'></th></span></q></dt></tr></i><div id='Cv95x'><tfoot id='Cv95x'></tfoot><dl id='Cv95x'><fieldset id='Cv95x'></fieldset></dl></div>

          <tbody id='Cv95x'></tbody>

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

                <tfoot id='Cv95x'></tfoot>
              1. <legend id='Cv95x'><style id='Cv95x'><dir id='Cv95x'><q id='Cv95x'></q></dir></style></legend>
              2. <small id='Cv95x'></small><noframes id='Cv95x'>

                • 本文介绍了计算python字典中某个值的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有这样的东西:

                  D = {'a': 97, 'c': 0 , 'b':0,'e': 94, 'r': 97 , 'g':0}
                  

                  如果我想例如将0"的出现次数计算为一个值而不必迭代整个列表,这是否可能以及如何实现?

                  If I want for example to count the number of occurrences for the "0" as a value without having to iterate the whole list, is that even possible and how?

                  推荐答案

                  如 THIS ANSWER 中所述,使用 operator.countOf() 是方式去,但你也可以在 sum() 函数中使用生成器,如下所示:

                  As mentioned in THIS ANSWER using operator.countOf() is the way to go but you can also use a generator within sum() function as following:

                  sum(value == 0 for value in D.values())
                  # Or the following which is more optimized 
                  sum(1 for v in D.values() if v == 0)
                  

                  或者作为一种稍微优化和功能性更强的方法,您可以通过将整数的 __eq__ 方法作为构造函数传递来使用 map 函数.

                  Or as a slightly more optimized and functional approach you can use map function by passing the __eq__ method of the integer as the constructor function.

                  sum(map((0).__eq__, D.values()))
                  

                  基准测试:

                  In [15]: D = dict(zip(range(1000), range(1000)))
                  
                  In [16]: %timeit sum(map((0).__eq__, D.values()))
                  49.6 s ± 770 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
                  
                  In [17]: %timeit sum(v==0 for v in D.values())
                  60.9 s ± 669 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
                  
                  In [18]: %timeit sum(1 for v in D.values() if v == 0)
                  30.2 s ± 515 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
                  
                  In [19]: %timeit countOf(D.values(), 0)
                  16.8 s ± 74.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
                  

                  请注意,虽然在这种情况下使用 map 函数可能会更优化,但为了对这两种方法有更全面和一般的想法,您还应该对相对较大的数据集运行基准测试.然后,您可以根据您拥有的数据结构和数量使用最合适的方法.

                  Note that although using map function in this case may be more optimized, but in order to have a more comprehensive and general idea about the two approaches you should run the benchmark for relatively large datasets as well. Then, you can use the most proper approach based on the structure and amount of data you have.

                  这篇关于计算python字典中某个值的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <tbody id='Jjtn9'></tbody>

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

                      <legend id='Jjtn9'><style id='Jjtn9'><dir id='Jjtn9'><q id='Jjtn9'></q></dir></style></legend>
                      <tfoot id='Jjtn9'></tfoot>
                        <bdo id='Jjtn9'></bdo><ul id='Jjtn9'></ul>

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