顺之利

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

问题:查找列表中指定的值的所有元素

使用list的index方法

 1 def find_index(src, key):
 2     start_pos = 0
 3     for i in range(src.count(key)):
 4         if start_pos == 0:
 5             start_pos = src.index(key)
 6         else:
 7             start_pos = src.index(key, start_pos+1)
 8         print(start_pos)
 9 
10 if __name__ == '__main__':
11     src = [1, 2, 3, 5, 2, 7, 2, 0, 3, 2]
12     key = -6
13     find_index(src, key)

 

posted on 2017-01-15 10:22  顺之利  阅读(1905)  评论(0编辑  收藏  举报