python中返回列表元素的索引

 

>>> a = ["aa","bb","cc","aa","dd","ee","aa","aa","ff","aa"]
>>> a
['aa', 'bb', 'cc', 'aa', 'dd', 'ee', 'aa', 'aa', 'ff', 'aa']
>>> a.index("aa")   ## 返回单元元素的索引
0
>>> c = a.count("aa")
>>> c
5
>>> b = -1
>>> for i in range(c):
    b = a.index("aa",b + 1,len(a))
    print("aa",b)    ## 返回重复元素的所有索引

    
aa 0
aa 3
aa 6
aa 7
aa 9

 

posted @ 2021-02-24 09:22  小鲨鱼2018  阅读(892)  评论(0编辑  收藏  举报