python中输出列表中重复元素的所有索引

 

>>> a = ["aa","bb","aa","cc","aa","dd","bb","aa","cc"]
>>> a
['aa', 'bb', 'aa', 'cc', 'aa', 'dd', 'bb', 'aa', 'cc']
>>> b = a.count("aa")
>>> b
4
>>> c = -1
>>> for i in range(b):
    c = a.index("aa",c + 1,len(a))
    print("aa",c)

    
aa 0
aa 2
aa 4
aa 7

 

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