【问题标题】:Most efficient way to get indexposition of a sublist in a nested list获取嵌套列表中子列表索引位置的最有效方法
【发布时间】:2014-06-02 21:03:15
【问题描述】:

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

【问题讨论】:

    标签: python list indexing nested-lists sublist


    【解决方案1】:

    是的。它叫做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
    

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多