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

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

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

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

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

        在python中处理list.index(可能不存在)的最佳方法?

        Best way to handle list.index(might-not-exist) in python?(在python中处理list.index(可能不存在)的最佳方法?)
        • <bdo id='S4zXi'></bdo><ul id='S4zXi'></ul>

            <tbody id='S4zXi'></tbody>
          <tfoot id='S4zXi'></tfoot>

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

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

                <i id='S4zXi'><tr id='S4zXi'><dt id='S4zXi'><q id='S4zXi'><span id='S4zXi'><b id='S4zXi'><form id='S4zXi'><ins id='S4zXi'></ins><ul id='S4zXi'></ul><sub id='S4zXi'></sub></form><legend id='S4zXi'></legend><bdo id='S4zXi'><pre id='S4zXi'><center id='S4zXi'></center></pre></bdo></b><th id='S4zXi'></th></span></q></dt></tr></i><div id='S4zXi'><tfoot id='S4zXi'></tfoot><dl id='S4zXi'><fieldset id='S4zXi'></fieldset></dl></div>
                  本文介绍了在python中处理list.index(可能不存在)的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码看起来像这样:

                  I have code which looks something like this:

                  thing_index = thing_list.index(thing)
                  otherfunction(thing_list, thing_index)
                  

                  好的,这很简单,但你明白了.现在 thing 可能实际上不在列表中,在这种情况下,我想将 -1 作为 thing_index 传递.在其他语言中,如果 index() 找不到元素,这就是您所期望的返回值.实际上它会抛出一个 ValueError.

                  ok so that's simplified but you get the idea. Now thing might not actually be in the list, in which case I want to pass -1 as thing_index. In other languages this is what you'd expect index() to return if it couldn't find the element. In fact it throws a ValueError.

                  我可以这样做:

                  try:
                      thing_index = thing_list.index(thing)
                  except ValueError:
                      thing_index = -1
                  otherfunction(thing_list, thing_index)
                  

                  但这感觉很脏,而且我不知道是否会因其他原因引发 ValueError.我根据生成器函数想出了以下解决方案,但看起来有点复杂:

                  But this feels dirty, plus I don't know if ValueError could be raised for some other reason. I came up with the following solution based on generator functions, but it seems a little complex:

                  thing_index = ( [(i for i in xrange(len(thing_list)) if thing_list[i]==thing)] or [-1] )[0]
                  

                  有没有更简洁的方法来实现同样的目标?假设列表没有排序.

                  Is there a cleaner way to achieve the same thing? Let's assume the list isn't sorted.

                  推荐答案

                  使用 try-except 子句并没有什么脏".这是pythonic的方式.ValueError 将仅由 .index 方法引发,因为它是您那里唯一的代码!

                  There is nothing "dirty" about using try-except clause. This is the pythonic way. ValueError will be raised by the .index method only, because it's the only code you have there!

                  回答评论:
                  在 Python 中,请求宽恕比获得许可更容易 理念已经确立,no index 不会针对任何其他问题引发此类错误.不是我能想到的.

                  To answer the comment:
                  In Python, easier to ask forgiveness than to get permission philosophy is well established, and no index will not raise this type of error for any other issues. Not that I can think of any.

                  这篇关于在python中处理list.index(可能不存在)的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)
                  <tfoot id='QGjmK'></tfoot>
                  • <bdo id='QGjmK'></bdo><ul id='QGjmK'></ul>

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

                        <tbody id='QGjmK'></tbody>

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

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