python查找列表中某個元素所有下標的兩個方法


方法一

摘自Python查找列表中某個元素返回所有下標

 

方法二

name = ['hello', 'world', 'a', 'b', 'c', 1, 2, 3, 'hello', 'world', 'a', 'b', 'c', 1, 2, 3]
first_pos = 0
for i in range(name.count('a')):
    new_list = name[first_pos:]
    next_pos = new_list.index(2) + 1
    print('find ', first_pos + new_list.index('a'))
    first_pos += next_pos

# 運行結果:
#   find  2
#   find  10

摘自:Python 查找list中的某個元素的所有的下標方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM