在嵌套列表中获取子列表的索引位置的最有效方法

use*_*431 0 python indexing list nested-lists sublist

我想得到一个嵌套列表中子列表的索引位置,例如nested [1]中的[1,2],[[1,2],[3,4]].获取子列表索引的最优雅方法是什么,是否有类似于index()的东西?

use*_*ica 5

是.它被称为index.

>>> [[1, 2], [3, 4]].index([1, 2])
0
>>> [[1, 2], [3, 4]].index([3, 4])
1
>>> [[1, 2], [3, 4]].index([1, 5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: [1, 5] is not in list
Run Code Online (Sandbox Code Playgroud)