Most efficient way to get indexposition of a sublist in a nested list

好久不见. 提交于 2019-12-25 03:16:40

问题


I want to get the indexposition of a sublist in a nested list, e.g 0 for [1,2] in nested_ls = [[1,2],[3,4]]. What's the most elegant way of getting the index of the sublist, is there something similar to index() ?


回答1:


Yes. It's called 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


来源:https://stackoverflow.com/questions/24003247/most-efficient-way-to-get-indexposition-of-a-sublist-in-a-nested-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!