带有多个参数的 list.index() 在 Python 2.x 中做什么?

Haw*_*r65 0 python arrays indexing

我不能肯定地说这是最快的方法:

i = -1
for j in xrange(n):
    i = x.index(True, i + 1)
Run Code Online (Sandbox Code Playgroud)

x:这里的布尔值列表

n: 出现次数

True: 搜索到的元素

在上面的代码中,i = x.index(True, i + 1)到底做了什么?

具体来说,第二个参数的作用是什么i + 1

我在 上找不到任何具有多个参数的示例list.index()

编辑:我正在使用 Python 2.7

Ash*_*ish 5

list.index(x[, start[, end]])
Run Code Online (Sandbox Code Playgroud)

返回列表中第一个值为 x 的项目的从零开始的索引。如果不存在这样的项目,则引发 ValueError。

可选参数 start 和 end 被解释为切片符号,并用于将搜索限制为列表的特定子序列。返回的索引是相对于完整序列的开头而不是起始参数计算的。

访问此文档链接以获取更多信息

fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']

fruits.index('banana')
Run Code Online (Sandbox Code Playgroud)

3

fruits.index('banana', 4)  # Find next banana starting a position 4
Run Code Online (Sandbox Code Playgroud)

6