Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python index()方法不接受无作为开始/停止_Python_Python 3.x - Fatal编程技术网

Python index()方法不接受无作为开始/停止

Python index()方法不接受无作为开始/停止,python,python-3.x,Python,Python 3.x,在为列表编写二进制搜索方法时,我决定在通过二进制搜索方法确定的列表的较小部分上使用内置的index()方法。然而,在某些情况下,我得到了错误: TypeError: slice indices must be integers or None or have an __index__ method 每当列表长度低于一定长度时,就会发生这种情况。导致它的代码是: def iter_chop(target, a_list): arr = a_list[:] start = None

在为列表编写二进制搜索方法时,我决定在通过二进制搜索方法确定的列表的较小部分上使用内置的
index()
方法。然而,在某些情况下,我得到了错误:

TypeError: slice indices must be integers or None or have an __index__ method
每当列表长度低于一定长度时,就会发生这种情况。导致它的代码是:

def iter_chop(target, a_list):
    arr = a_list[:]
    start = None
    stop = None
    <snip>
    return a_list.index(target, start, stop)
所以我假设调用
a.index(3,None,None)
相当于调用
a[None:None].index(3)
。然而:

>>> a.index(3,None,None)
Traceback (most recent call last):
  File "<pyshell#199>", line 1, in <module>
    a.index(3,None,None)
TypeError: slice indices must be integers or None or have an __index__ method
a.索引(3,无,无) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 a、 索引(3,无,无) TypeError:切片索引必须是整数或无,或具有_索引_方法
我是否错误地假设无论错误消息指示什么,
index()
的参数都是如何处理的?这是一个bug吗?

这是一个解析参数的bug。有关字符串方法的相同错误,请参见和,以及列表

是的,文档表明索引的使用方式与切片大致相同,用于解析这些参数的内部实用程序函数给出了错误消息


列表错误似乎与API中的更改以及哪些版本可以得到修复有关,或者,如果错误消息应该被修复,则列表错误可能会被挂起。

Hrm,文档确实给人的印象是,
None
可以工作,因为它们声明它大致相当于使用
s[i:j].index(x)
。在这种情况下,这是一个文档错误。“错误消息表明没有一个参数是可接受的”。事实上,我认为没有。我认为
index
的源代码包含了另一个引起错误的函数。因此,内部函数可以接受整数或无,而不是索引本身。(我同意Martijn的观点,即这些文档在任何情况下都有一定的误导性)如果您尝试这样做,您会得到相同的错误消息
[1,2,3][0:'s']
,因此我可能天真地认为引擎盖下正在进行一些切片。@Kevin:事实上,错误消息来自,它假定调用方已经测试了
。但政府并没有这样做。它不支持
None
@IanAuld:正在使用一个用于切片的实用功能,这与切片完全不同。他们是否提到他们将为自行车棚喷漆的颜色?
>>> a.index(3,None,None)
Traceback (most recent call last):
  File "<pyshell#199>", line 1, in <module>
    a.index(3,None,None)
TypeError: slice indices must be integers or None or have an __index__ method