python中返回列表中特定指定元素的所有索引


python中返回列表中指定元素的所有索引。

1、基本用法

>>> test1 ['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'xx', 'bb'] >>> test1.index("aa") 0

 

2、返回所有索引

>>> test1 ['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'xx', 'bb'] >>> test1.count("aa") 3
>>> tmp = -1
>>> for i in range(test1.count("aa")): tmp = test1.index("aa",tmp + 1,len(test1)) print("aa",tmp) aa 0 aa 2 aa 4

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM