Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python .index()显示列表中两个相似元素的索引_Python_Indexing - Fatal编程技术网

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]