在嵌套列表中获取子列表的索引位置的最有效方法 [英] Most efficient way to get indexposition of a sublist in a nested list

查看:624
本文介绍了在嵌套列表中获取子列表的索引位置的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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() ?

推荐答案

是的。它被称为 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

这篇关于在嵌套列表中获取子列表的索引位置的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆