1. <tfoot id='QkqTt'></tfoot>

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

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

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

          <bdo id='QkqTt'></bdo><ul id='QkqTt'></ul>
      2. 优化的切割/切片排序列表的方法

        Optimized method of cutting/slicing sorted lists(优化的切割/切片排序列表的方法)
          <bdo id='Ky5Cb'></bdo><ul id='Ky5Cb'></ul>
            <tbody id='Ky5Cb'></tbody>
          <legend id='Ky5Cb'><style id='Ky5Cb'><dir id='Ky5Cb'><q id='Ky5Cb'></q></dir></style></legend>

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

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

                1. <tfoot id='Ky5Cb'></tfoot>
                2. 本文介绍了优化的切割/切片排序列表的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中是否有任何预制的优化工具/库来剪切/切片小于"值的列表?

                  Is there any pre-made optimized tool/library in Python to cut/slice lists for values "less than" something?

                  问题来了:假设我有一个类似的列表:

                  Here's the issue: Let's say I have a list like:

                  a=[1,3,5,7,9]
                  

                  我想删除所有 <= 6 的数字,所以结果列表将是

                  and I want to delete all the numbers which are <= 6, so the resulting list would be

                  [7,9]
                  

                  6 不在列表中,所以我不能使用列表的内置 index(6) 方法.我可以这样做:

                  6 is not in the list, so I can't use the built-in index(6) method of the list. I can do things like:

                  #!/usr/bin/env python
                  a = [1, 3, 5, 7, 9]
                  cut=6
                  for i in range(len(a)-1, -2, -1):
                      if a[i] <= cut:
                          break
                  b = a[i+1:]
                  print "Cut list: %s" % b
                  

                  如果要从中剪切的索引接近列表的末尾,这将是一种相当快捷的方法,但如果项目接近列表的开头(假设我想删除所有>2的项目,会有很多迭代).

                  which would be fairly quick method if the index to cut from is close to the end of the list, but which will be inefficient if the item is close to the beginning of the list (let's say, I want to delete all the items which are >2, there will be a lot of iterations).

                  我也可以使用二进制搜索等实现我自己的 find 方法,但我想知道是否有一个更广泛的内置库来处理我可以在其他情况下重用的此类事物(例如, 如果我需要删除所有 >=6).

                  I can also implement my own find method using binary search or such, but I was wondering if there's a more... wide-scope built in library to handle this type of things that I could reuse in other cases (for instance, if I need to delete all the number which are >=6).

                  提前谢谢你.

                  推荐答案

                  你可以使用 bisect 模块 执行排序搜索:

                  You can use the bisect module to perform a sorted search:

                  >>> import bisect
                  >>> a[bisect.bisect_left(a, 6):]
                  [7, 9]
                  

                  这篇关于优化的切割/切片排序列表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

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

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

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