遍历列表,找出具体元素的索引

来源:互联网 发布:三毛梦里花落知多少txt 编辑:程序博客网 时间:2024/04/30 10:20

问:从列表中找出所有数字2的索引号:

name = ['d', 'a', 'b', 'c', 'd', 2, 'b', 'c', '2', 2, 'b', 'a', 2, 2, 'a', 'hello', 'world', 2, 'peng']


first_position = 0for i in range(name.count(2)):    next_position = name[first_position:].index(2)    print 'the index is: ', next_position + first_position    first_position += next_position + 1


0 0
原创粉丝点击