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

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

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

        <tfoot id='iFogl'></tfoot>
      2. Python中的序列查找函数

        Sequence find function in Python(Python中的序列查找函数)
        • <tfoot id='ePKQp'></tfoot>
              <tbody id='ePKQp'></tbody>

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

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

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

                  <i id='ePKQp'><tr id='ePKQp'><dt id='ePKQp'><q id='ePKQp'><span id='ePKQp'><b id='ePKQp'><form id='ePKQp'><ins id='ePKQp'></ins><ul id='ePKQp'></ul><sub id='ePKQp'></sub></form><legend id='ePKQp'></legend><bdo id='ePKQp'><pre id='ePKQp'><center id='ePKQp'></center></pre></bdo></b><th id='ePKQp'></th></span></q></dt></tr></i><div id='ePKQp'><tfoot id='ePKQp'></tfoot><dl id='ePKQp'><fieldset id='ePKQp'></fieldset></dl></div>
                  本文介绍了Python中的序列查找函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在满足特定条件的序列中找到对象?

                  How do I find an object in a sequence satisfying a particular criterion?

                  列表理解和过滤器会遍历整个列表.唯一的选择是手工循环吗?

                  List comprehension and filter go through the entire list. Is the only alternative a handmade loop?

                  mylist = [10, 2, 20, 5, 50]
                  find(mylist, lambda x:x>10) # Returns 20
                  

                  推荐答案

                  这是我使用的模式:

                  mylist = [10, 2, 20, 5, 50]
                  found = next(i for i in mylist if predicate(i))
                  

                  或者,在 Python 2.4/2.5 中,next() 不是内置函数:

                  Or, in Python 2.4/2.5, next() is a not a builtin:

                  found = (i for i in mylist if predicate(i)).next()
                  

                  请注意,如果没有找到元素,next() 会引发 StopIteration.在大多数情况下,这可能很好.您要求第一个元素,不存在这样的元素,因此程序可能无法继续.

                  Do note that next() raises StopIteration if no element was found. In most cases, that's probably good. You asked for the first element, no such element exists, and so the program probably cannot continue.

                  另一方面,如果您确实知道在这种情况下该怎么做,则可以为 next() 提供默认值:

                  If, on the other hand, you do know what to do in that case, you can supply a default to next():

                  conf_files = ['~/.foorc', '/etc/foorc']
                  conf_file = next((f for f in conf_files if os.path.exists(f)),
                                   '/usr/lib/share/foo.defaults')
                  

                  这篇关于Python中的序列查找函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='fXjYT'></bdo><ul id='fXjYT'></ul>
                            <tbody id='fXjYT'></tbody>
                          <legend id='fXjYT'><style id='fXjYT'><dir id='fXjYT'><q id='fXjYT'></q></dir></style></legend>

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