小编Aks*_*hay的帖子

Python最快的方法来查找列表中项目的索引

如果一个人想要在列表中找到项目的索引,你可以用两种不同的方式来做这就是我所知道的最快的

aList = [123, 'xyz', 'zara','xyz', 'abc']; 
indices = [i for i, x in enumerate(aList) if x == "xyz"]
print(indices)
Run Code Online (Sandbox Code Playgroud)

另一种方式不是pythonic和慢

count = 0
indices = []
aList = [123, 'xyz', 'zara','xyz', 'abc'];
for i in range(0,len(aList):
    if 'xyz' == aList[i]:
        indices.append(i)
print(indices)
Run Code Online (Sandbox Code Playgroud)

第一种方法无疑是更快但是如果你想要更快的话有什么方法呢?对于第一个索引使用方法

aList = [123, 'xyz', 'zara','xyz', 'abc'];             
print "Index for xyz : ", aList.index( 'xyz' ) 
Run Code Online (Sandbox Code Playgroud)

是非常快但无法处理多个索引如何加快速度?

python indexing performance list

2
推荐指数
2
解决办法
2万
查看次数

标签 统计

indexing ×1

list ×1

performance ×1

python ×1