Python .index()显示列表中两个相似元素的索引
假设我有一张清单Python .index()显示列表中两个相似元素的索引,python,indexing,Python,Indexing,假设我有一张清单 list = [1,2,1,3,4,5] 如何使用python中的.index()函数(或任何其他方法)从列表中查找1的索引 >>> lst = [1,2,1,3,4,5] >>> index_one = [i for i, x in enumerate(lst) if x == 1] >>> index_one [0, 2]
list = [1,2,1,3,4,5]
如何使用python中的.index()
函数(或任何其他方法)从列表中查找1
的索引
>>> lst = [1,2,1,3,4,5]
>>> index_one = [i for i, x in enumerate(lst) if x == 1]
>>> index_one
[0, 2]