查找lis中第n项的索引

2024-06-25 05:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到列表中第n个项目的索引。e、 g

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

第n个真的指数是多少?如果我想要第五次出现(第四次如果零索引),答案是10。

我想到了:

indargs = [ i for i,a in enumerate(x) if a ]
indargs[n]

请注意,x.index返回某个点之后的第一次出现或第一次出现,因此就我所知不是解决方案。

在numpy中也有类似的解决方案,例如使用cumsumwhere,但是我想知道是否有一种无numpy的方法来解决这个问题。

我很担心性能,因为我第一次遇到这个问题是在为一个Project Euler问题实现一个Eratosthenes筛时,但这是我在其他情况下遇到的一个更一般的问题。

编辑:我得到了很多很好的答案,所以我决定做一些性能测试。下面是以秒为单位的timeit执行时间,对于搜索第4000个/1000个真值的len元素的列表。列表是随机的真/假。下面链接的源代码;有点混乱。我使用海报名称的简短/修改版本来描述除了listcomp之外的功能,这是上面简单的列表理解。

True Test (100'th True in a list containing True/False)
         nelements      eyquem_occur eyquem_occurrence            graddy            taymon          listcomp       hettinger26         hettinger
             3000:          0.007824          0.031117          0.002144          0.007694          0.026908          0.003563          0.003563
            10000:          0.018424          0.103049          0.002233          0.018063          0.088245          0.003610          0.003769
            50000:          0.078383          0.515265          0.002140          0.078074          0.442630          0.003719          0.003608
           100000:          0.152804          1.054196          0.002129          0.152691          0.903827          0.003741          0.003769
           200000:          0.303084          2.123534          0.002212          0.301918          1.837870          0.003522          0.003601
True Test (1000'th True in a list containing True/False)
         nelements      eyquem_occur eyquem_occurrence            graddy            taymon          listcomp       hettinger26         hettinger
             3000:          0.038461          0.031358          0.024167          0.039277          0.026640          0.035283          0.034482
            10000:          0.049063          0.103241          0.024120          0.049383          0.088688          0.035515          0.034700
            50000:          0.108860          0.516037          0.023956          0.109546          0.442078          0.035269          0.035373
           100000:          0.183568          1.049817          0.024228          0.184406          0.906709          0.035135          0.036027
           200000:          0.333501          2.141629          0.024239          0.333908          1.826397          0.034879          0.036551
True Test (20000'th True in a list containing True/False)
         nelements      eyquem_occur eyquem_occurrence            graddy            taymon          listcomp       hettinger26         hettinger
             3000:          0.004520          0.004439          0.036853          0.004458          0.026900          0.053460          0.053734
            10000:          0.014925          0.014715          0.126084          0.014864          0.088470          0.177792          0.177716
            50000:          0.766154          0.515107          0.499068          0.781289          0.443654          0.707134          0.711072
           100000:          0.837363          1.051426          0.501842          0.862350          0.903189          0.707552          0.706808
           200000:          0.991740          2.124445          0.498408          1.008187          1.839797          0.715844          0.709063
Number Test (750'th 0 in a list containing 0-9)
         nelements      eyquem_occur eyquem_occurrence            graddy            taymon          listcomp       hettinger26         hettinger
             3000:          0.026996          0.026887          0.015494          0.030343          0.022417          0.026557          0.026236
            10000:          0.037887          0.089267          0.015839          0.040519          0.074941          0.026525          0.027057
            50000:          0.097777          0.445236          0.015396          0.101242          0.371496          0.025945          0.026156
           100000:          0.173794          0.905993          0.015409          0.176317          0.762155          0.026215          0.026871
           200000:          0.324930          1.847375          0.015506          0.327957          1.536012          0.027390          0.026657

Hettinger的itertools解决方案几乎总是最好的。taymon和graddy的解决方案在大多数情况下都是次优的,不过当您希望第n个实例的n较高或出现次数少于n的列表时,列表理解方法可以更好地用于短数组。如果出现次数少于n次,则初始的count检查将节省时间。而且,graddy在搜索数字时比True/False更有效。。。不清楚原因。eyquem的解在本质上与其他解等价,开销略大或略小;eyquem_-occure与taymon的解大致相同,而eyquem_-occurrence与listcomp相似。


Tags: intestfalsetrue列表解决方案listcontaining
3条回答

我不能肯定这是最快的方法,但我想这是很好的:

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

答案是i

[y for y in enumerate(x) if y[1]==True][z][0]

注:这里Z是第n次出现

@Taymon使用list.index得到的答案非常好。

FWIW,这里有一个使用itertools module的函数方法。它可以处理任何iterable输入,而不仅仅是列表:

>>> from itertools import compress, count, imap, islice
>>> from functools import partial
>>> from operator import eq

>>> def nth_item(n, item, iterable):
        indicies = compress(count(), imap(partial(eq, item), iterable))
        return next(islice(indicies, n, None), -1)

这个例子很好,因为它展示了如何有效地组合Python的功能工具集。注意,一旦建立了管道,Python的eval循环就不会有任何变化——所有的事情都是以C速度完成的,内存占用很小,计算很慢,没有变量分配,组件可以单独测试。听着,这是函数式程序员梦寐以求的一切:-)

样本运行:

>>> x = [False,True,True,False,True,False,True,False,False,False,True,False,True]
>>> nth_item(50, True, x)
-1
>>> nth_item(0, True, x)
1
>>> nth_item(1, True, x)
2
>>> nth_item(2, True, x)
4
>>> nth_item(3, True, x)
6

相关问题 更多 >