Python Fastest way to find Indexes of item in list

前端 未结 6 1916
半阙折子戏
半阙折子戏 2020-12-19 06:50

If one was to attempt to find the indexes of an item in a list you could do it a couple different ways here is what I know to be the fastest

aList = [123, \'         


        
6条回答
  •  被撕碎了的回忆
    2020-12-19 07:02

    D=dict()
    for i, item in enumerate(l):
        if item not in D:
            D[item] = [i]
        else:
            D[item].append(i)
    

    Then simply call D[item] to get the indices that match. You'll give up initial calculation time but gain it during call time.

提交回复
热议问题