在Python中,要寻找列表中元素的下标,你可以使用以下几种方法:
1. 使用`enumerate`函数:
numbers = [10, 29, 30, 41]for index, value in enumerate(numbers):print('({0}, {1})'.format(index, value))
2. 使用列表推导式结合`enumerate`:
word = ['dislike', ' is ', 'life', 'like', ' long ', 'python', 'short', 'too', ' is ']word1 = [(i, x) for i, x in enumerate(word) if x == ' is ']print(word1)

3. 使用`list.index()`方法查找特定元素的下标:
name = ['hello', 'world', 'a', 'b', 'c', 1, 2, 3, 'hello', 'world', 'a', 'b', 'c', 1, 2, 3]first_pos = 0for i in range(name.count(2)):new_list = name[first_pos:]next_pos = new_list.index(2) + 1print('find', first_pos, next_pos)first_pos = next_pos
4. 使用`list.count()`方法结合循环查找所有特定元素的下标:
name = ['hello', 'world', 'a', 'b', 'c', 1, 2, 3, 'hello', 'world', 'a', 'b', 'c', 1, 2, 3]first_pos = 0while first_pos < len(name):pos = name.index(2, first_pos)print('find', first_pos, pos)first_pos = pos + 1
