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

<tfoot id='mMI4P'></tfoot>

      • <bdo id='mMI4P'></bdo><ul id='mMI4P'></ul>

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

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

      3. 弹出索引超出范围

        Pop index out of range(弹出索引超出范围)

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

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

              <legend id='gOzkr'><style id='gOzkr'><dir id='gOzkr'><q id='gOzkr'></q></dir></style></legend>
                  <tbody id='gOzkr'></tbody>

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

                  本文介绍了弹出索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  N=8
                  f,g=4,7
                  indexList = range(N)
                  print indexList
                  print f, g
                  indexList.pop(f)
                  indexList.pop(g)
                  

                  在此代码中,我收到一条错误消息,指出 indexListg 的弹出索引超出范围.这是输出:

                  In this code I am getting an error stating that the pop index of g in indexList is out of range. Here is the output:

                  [0, 1, 2, 3, 4, 5, 6, 7]
                  4 7
                  Traceback (most recent call last):
                  indexList.pop(g)
                  IndexError: pop index out of range
                  

                  我不明白,g 的值为 7,列表包含 7 个值,为什么无法返回列表中的 7?

                  I don't understand, g has a value of 7, the list contains 7 values, why is it not able to return me the 7 in the list?

                  推荐答案

                  要获取弹出列表的最终值,可以这样:

                  To get the final value of a list pop'ed, you can do it this way:

                  >>> l=range(8)
                  >>> l
                  [0, 1, 2, 3, 4, 5, 6, 7]
                  >>> l.pop(4)                    # item at index 4
                  4
                  >>> l
                  [0, 1, 2, 3, 5, 6, 7]
                  >>> l.pop(-1)                   # item at end - equivalent to pop()
                  7
                  >>> l
                  [0, 1, 2, 3, 5, 6]
                  >>> l.pop(-2)                   # one left of the end 
                  5
                  >>> l
                  [0, 1, 2, 3, 6]
                  >>> l.pop()                     # always the end item
                  6
                  >>> l
                  [0, 1, 2, 3]
                  

                  请记住,pop 会删除该项目,并且列表在弹出后更改长度.使用负数从可能改变大小的列表末尾开始索引,或者只使用不带参数的 pop() 结束项.

                  Keep in mind that pop removes the item, and the list changes length after the pop. Use negative numbers to index from the end of a list that may be changing in size, or just use pop() with no arguments for the end item.

                  由于 pop 会产生这些错误,因此您经常会在 异常块:

                  Since a pop can produce these errors, you often see them in an exception block:

                  >>> l=[]
                  >>> try:
                  ...    i=l.pop(5)
                  ... except IndexError:
                  ...    print "sorry -- can't pop that"
                  ... 
                  sorry -- can't pop that
                  

                  这篇关于弹出索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                • <legend id='GMI2e'><style id='GMI2e'><dir id='GMI2e'><q id='GMI2e'></q></dir></style></legend>
                  <i id='GMI2e'><tr id='GMI2e'><dt id='GMI2e'><q id='GMI2e'><span id='GMI2e'><b id='GMI2e'><form id='GMI2e'><ins id='GMI2e'></ins><ul id='GMI2e'></ul><sub id='GMI2e'></sub></form><legend id='GMI2e'></legend><bdo id='GMI2e'><pre id='GMI2e'><center id='GMI2e'></center></pre></bdo></b><th id='GMI2e'></th></span></q></dt></tr></i><div id='GMI2e'><tfoot id='GMI2e'></tfoot><dl id='GMI2e'><fieldset id='GMI2e'></fieldset></dl></div>

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

                      <tfoot id='GMI2e'></tfoot>

                            <bdo id='GMI2e'></bdo><ul id='GMI2e'></ul>
                              <tbody id='GMI2e'></tbody>