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

        <legend id='WiarO'><style id='WiarO'><dir id='WiarO'><q id='WiarO'></q></dir></style></legend>
      2. 获取布尔数组中至少 n 个连续 False 值的第一个块的索引

        get index of the first block of at least n consecutive False values in boolean array(获取布尔数组中至少 n 个连续 False 值的第一个块的索引)
        <legend id='RurDs'><style id='RurDs'><dir id='RurDs'><q id='RurDs'></q></dir></style></legend>
            <bdo id='RurDs'></bdo><ul id='RurDs'></ul>
          • <small id='RurDs'></small><noframes id='RurDs'>

            • <tfoot id='RurDs'></tfoot>
                  <tbody id='RurDs'></tbody>

                1. <i id='RurDs'><tr id='RurDs'><dt id='RurDs'><q id='RurDs'><span id='RurDs'><b id='RurDs'><form id='RurDs'><ins id='RurDs'></ins><ul id='RurDs'></ul><sub id='RurDs'></sub></form><legend id='RurDs'></legend><bdo id='RurDs'><pre id='RurDs'><center id='RurDs'></center></pre></bdo></b><th id='RurDs'></th></span></q></dt></tr></i><div id='RurDs'><tfoot id='RurDs'></tfoot><dl id='RurDs'><fieldset id='RurDs'></fieldset></dl></div>
                  本文介绍了获取布尔数组中至少 n 个连续 False 值的第一个块的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 numpy 布尔数组

                  w=np.array([True,False,True,True,False,False,False])
                  

                  我想获取第一次有 n_at_least 错误值的索引.比如这里

                  I would like to get the index of the first time there are at n_at_least false values. For instance here

                  `n_at_least`=1 -> desired_index=1
                  
                  `n_at_least`=3 -> desired_index=4
                  

                  我试过了

                  np.cumsum(~w)
                  

                  每次遇到 False 值时都会增加.但是,当遇到 True 时,计数器不再从 0 开始,所以我只得到 False 元素的总数,而不是最后一个连续元素的计数.

                  which does increase every time a False value is encountered. However, when True is encountered the counter is not starting from 0 again so I only get the total count of False elements rather than the count of the last consecutive ones.

                  推荐答案

                  我认为对于这种线性搜索操作,python 实现是可以的.我的建议是这样的:

                  I think for this linear search operation a python implementation is ok. My suggestion looks like this:

                  def find_block(arr, n_at_least=1):
                      current_index = 0
                      current_count = 0
                      for index, item in enumerate(arr):
                           if item:
                               current_count = 0
                               current_index = index + 1
                           else:
                               current_count += 1
                           if current_count == n_at_least:
                               return current_index
                      return None # Make sure this is outside for loop
                  

                  运行此函数会产生以下输出:

                  Running this function yields the following outputs:

                  >>> import numpy
                  >>> w = numpy.array([True, False, True, True, False, False, False])
                  >>> find_block(w, n_at_least=1)
                  1
                  >>> find_block(w, n_at_least=3)
                  4
                  >>> find_block(w, n_at_least=4)
                  >>> # None
                  

                  这篇关于获取布尔数组中至少 n 个连续 False 值的第一个块的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

                            <legend id='ijzUm'><style id='ijzUm'><dir id='ijzUm'><q id='ijzUm'></q></dir></style></legend>
                          • <small id='ijzUm'></small><noframes id='ijzUm'>

                              <tbody id='ijzUm'></tbody>