Find the index of the n'th item in a list

后端 未结 11 1875
余生分开走
余生分开走 2020-12-13 00:04

I want to find the index of the n\'th occurrence of an item in a list. e.g.,

x=[False,True,True,False,True,False,True,False,False,False,True,False,True]
         


        
11条回答
  •  我在风中等你
    2020-12-13 00:50

    I can't say for certain that this is the fastest way, but I imagine it'd be pretty good:

    i = -1
    for j in xrange(n):
        i = x.index(True, i + 1)
    

    The answer is i.

提交回复
热议问题