【问题标题】:Pythonic way to find index of n th occurance of a value in a list? [duplicate]Pythonic方法来查找列表中第n个值的索引? [复制]
【发布时间】:2012-01-10 08:55:29
【问题描述】:

这是我的清单

list=[1,2,1,1,1,1,1,4,5]

现在我想以 Python 的方式知道第 5 个“1”的索引。

【问题讨论】:

    标签: python


    【解决方案1】:

    枚举可以在这里提供帮助

    [x for x in enumerate(list) if x[1]==1][5][0]
    
    #X[1]==1  implies values of element equals 1, since x[0] will be enumeration index.
    

    【讨论】:

      【解决方案2】:
      [i for (i,e) in enumerate(list) if e==1][5]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-05
        • 2012-01-10
        • 1970-01-01
        • 2010-11-27
        • 2016-09-26
        • 1970-01-01
        • 2012-11-18
        相关资源
        最近更新 更多