<tfoot id='7MCYV'></tfoot>
  • <legend id='7MCYV'><style id='7MCYV'><dir id='7MCYV'><q id='7MCYV'></q></dir></style></legend>
        • <bdo id='7MCYV'></bdo><ul id='7MCYV'></ul>

        <small id='7MCYV'></small><noframes id='7MCYV'>

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

        在一定条件下从列表(或其他可迭代的)中获取项目数

        Get number of items from list (or other iterable) with certain condition(在一定条件下从列表(或其他可迭代的)中获取项目数)
        <i id='Sj0gO'><tr id='Sj0gO'><dt id='Sj0gO'><q id='Sj0gO'><span id='Sj0gO'><b id='Sj0gO'><form id='Sj0gO'><ins id='Sj0gO'></ins><ul id='Sj0gO'></ul><sub id='Sj0gO'></sub></form><legend id='Sj0gO'></legend><bdo id='Sj0gO'><pre id='Sj0gO'><center id='Sj0gO'></center></pre></bdo></b><th id='Sj0gO'></th></span></q></dt></tr></i><div id='Sj0gO'><tfoot id='Sj0gO'></tfoot><dl id='Sj0gO'><fieldset id='Sj0gO'></fieldset></dl></div>

      2. <small id='Sj0gO'></small><noframes id='Sj0gO'>

            <tbody id='Sj0gO'></tbody>

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

              <tfoot id='Sj0gO'></tfoot>

                • <bdo id='Sj0gO'></bdo><ul id='Sj0gO'></ul>
                  本文介绍了在一定条件下从列表(或其他可迭代的)中获取项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有一个包含大量项目的列表,

                  l = [ 1, 4, 6, 30, 2, ... ]
                  

                  我要获取该列表中的项目数,其中一个项目满足特定条件。我的第一个想法是:

                  count = len([i for i in l if my_condition(l)])
                  
                  但是,如果过滤后的列表也有大量的项目,我认为 为过滤结果创建新列表只是浪费内存。为了提高效率,IMHO,上面的调用不能优于:

                  count = 0
                  for i in l:
                      if my_condition(l):
                          count += 1
                  

                  有没有什么函数式方法可以在不生成临时列表的情况下获取满足条件的项目数?

                  推荐答案

                  您可以使用generator expression:

                  >>> l = [1, 3, 7, 2, 6, 8, 10]
                  >>> sum(1 for i in l if i % 4 == 3)
                  2
                  

                  甚至

                  >>> sum(i % 4 == 3 for i in l)
                  2
                  

                  它使用的事实是True == 1False == 0

                  或者,您也可以使用itertools.imap(Python2)或简单的map(Python3):

                  >>> def my_condition(x):
                  ...     return x % 4 == 3
                  ... 
                  >>> sum(map(my_condition, l))
                  2
                  

                  这篇关于在一定条件下从列表(或其他可迭代的)中获取项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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;?(为什么我的函数输出打印出“无?)

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

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

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