给定一个列表[“foo”、“bar”、“baz”]和列表“bar”中的一个项,如何获取其索引1?
当前回答
index()返回值的第一个索引!
|索引(…)|L.index(value,[start,[stop]])->integer--返回值的第一个索引
def all_indices(value, qlist):
indices = []
idx = -1
while True:
try:
idx = qlist.index(value, idx+1)
indices.append(idx)
except ValueError:
break
return indices
all_indices("foo", ["foo","bar","baz","foo"])
其他回答
另一种选择
>>> a = ['red', 'blue', 'green', 'red']
>>> b = 'red'
>>> offset = 0;
>>> indices = list()
>>> for i in range(a.count(b)):
... indices.append(a.index(b,offset))
... offset = indices[-1]+1
...
>>> indices
[0, 3]
>>>
>>> ["foo", "bar", "baz"].index("bar")
1
有关列表的内置.index()方法,请参阅文档:
list.index(x[,start[,end]])返回值等于x的第一项列表中从零开始的索引。如果没有该项,则引发ValueError。可选参数start和end被解释为切片符号,用于将搜索限制在列表的特定子序列。返回的索引是相对于完整序列的开头而不是start参数计算的。
注意事项
列表长度的线性时间复杂性
索引调用按顺序检查列表中的每个元素,直到找到匹配项。如果列表很长,并且不能保证值会接近开始,这会降低代码的速度。
只有使用不同的数据结构才能完全避免这个问题。但是,如果已知元素在列表的某个部分内,则可以使用start和end参数来缩小搜索范围。
例如:
>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514
第二次调用速度快了几个数量级,因为它只需要搜索10个元素,而不是全部100万个元素。
仅返回第一个匹配项的索引
对索引的调用按顺序搜索列表,直到找到匹配项,然后停止。如果该值可能出现多次,并且需要所有索引,则索引无法解决问题:
>>> [1, 1].index(1) # the `1` index is not found.
0
相反,使用列表理解或生成器表达式进行搜索,使用enumerate获取索引:
>>> # A list comprehension gives a list of indices directly:
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> # A generator comprehension gives us an iterable object...
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> # which can be used in a `for` loop, or manually iterated with `next`:
>>> next(g)
0
>>> next(g)
2
如果只有一个匹配项,列表理解和生成器表达式技术仍然有效,并且更具通用性。
如果不匹配,则引发异常
如以上文档中所述,如果搜索的值不在列表中,则使用.index将引发异常:
>>> [1, 1].index(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 2 is not in list
如果这是一个问题,请首先使用my_list中的项显式检查,或者根据需要使用try/except处理异常。
显式检查简单易读,但它必须再次迭代列表。参见Python中的EAFP原理是什么?以获取有关此选择的更多指导。
如果性能令人担忧:
在许多答案中都提到,list.index(item)方法的内置方法是一个O(n)算法。如果您需要执行一次,这是很好的。但是,如果您需要多次访问元素的索引,那么首先创建一个项目索引对的字典(O(n)),然后在每次需要时访问O(1)处的索引更有意义。
如果您确定列表中的项目从未重复,您可以轻松地:
myList = ["foo", "bar", "baz"]
# Create the dictionary
myDict = dict((e,i) for i,e in enumerate(myList))
# Lookup
myDict["bar"] # Returns 1
# myDict.get("blah") if you don't want an error to be raised if element not found.
如果您可能有重复的元素,并且需要返回它们的所有索引:
from collections import defaultdict as dd
myList = ["foo", "bar", "bar", "baz", "foo"]
# Create the dictionary
myDict = dd(list)
for i,e in enumerate(myList):
myDict[e].append(i)
# Lookup
myDict["foo"] # Returns [0, 4]
在Python中查找给定列表中包含项的索引对于列表[“foo”、“bar”、“baz”]和列表“bar”中的项,在Python中获取其索引(1)的最干净方法是什么?
当然,有index方法,它返回第一次出现的索引:
>>> l = ["foo", "bar", "baz"]
>>> l.index('bar')
1
这种方法有几个问题:
如果该值不在列表中,您将得到ValueError如果列表中有多个值,则只获取第一个值的索引
没有值
如果值可能丢失,则需要捕获ValueError。
您可以这样使用可重用定义:
def index(a_list, value):
try:
return a_list.index(value)
except ValueError:
return None
然后这样使用:
>>> print(index(l, 'quux'))
None
>>> print(index(l, 'bar'))
1
这样做的缺点是,您可能需要检查返回的值是否为None:
result = index(a_list, value)
if result is not None:
do_something(result)
列表中有多个值
如果您可能会出现更多情况,则无法通过list.index获得完整信息:
>>> l.append('bar')
>>> l
['foo', 'bar', 'baz', 'bar']
>>> l.index('bar') # nothing at index 3?
1
您可以在列表中列举索引:
>>> [index for index, v in enumerate(l) if v == 'bar']
[1, 3]
>>> [index for index, v in enumerate(l) if v == 'boink']
[]
如果没有出现,则可以通过结果的布尔检查进行检查,或者在循环结果时不执行任何操作:
indexes = [index for index, v in enumerate(l) if v == 'boink']
for index in indexes:
do_something(index)
使用熊猫更好地处理数据
如果您有熊猫,您可以通过Series对象轻松获取此信息:
>>> import pandas as pd
>>> series = pd.Series(l)
>>> series
0 foo
1 bar
2 baz
3 bar
dtype: object
比较检查将返回一系列布尔值:
>>> series == 'bar'
0 False
1 True
2 False
3 True
dtype: bool
通过下标符号将该系列布尔值传递给该系列,您将得到匹配的成员:
>>> series[series == 'bar']
1 bar
3 bar
dtype: object
如果只需要索引,index属性将返回一系列整数:
>>> series[series == 'bar'].index
Int64Index([1, 3], dtype='int64')
如果您希望它们在列表或元组中,只需将它们传递给构造函数:
>>> list(series[series == 'bar'].index)
[1, 3]
是的,你也可以将列表理解与enumerate一起使用,但在我看来,这并不是那么优雅——你在Python中进行等式测试,而不是让用C编写的内置代码来处理:
>>> [i for i, value in enumerate(l) if value == 'bar']
[1, 3]
这是XY问题吗?
XY问题是询问您尝试的解决方案,而不是实际问题。
为什么您认为需要列表中给定元素的索引?
如果你已经知道它的价值,为什么你会在意它在列表中的位置?
如果值不存在,则捕获ValueError相当冗长,我更希望避免这种情况。
无论如何,我通常都会遍历列表,所以我通常会保留一个指向任何有趣信息的指针,用enumerate获取索引。
如果你在处理数据,你可能应该使用panda,它的工具比我展示的纯Python解决方案要优雅得多。
我不记得自己需要list.index。然而,我已经浏览了Python标准库,并看到了它的一些优秀用途。
它在idlelib中有很多用途,用于GUI和文本解析。
关键字模块使用它在模块中查找注释标记,以通过元编程自动重新生成其中的关键字列表。
在Lib/mailbox.py中,它似乎像有序映射一样使用它:
key_list[key_list.index(old)] = new
and
del key_list[key_list.index(key)]
在Lib/html/cookiejar.py中,似乎用于获取下一个月:
mon = MONTHS_LOWER.index(mon.lower())+1
在Lib/tarfile.py中,类似于distutils,获取一个项目的切片:
members = members[:members.index(tarinfo)]
在Lib/pickletools.py中:
numtopop = before.index(markobject)
这些用法的共同点是,它们似乎对大小受限的列表进行操作(这一点很重要,因为list.index的查找时间为O(n)),并且它们主要用于解析(在Idle的情况下为UI)。
虽然有它的用例,但它们相当罕见。如果您发现自己正在寻找这个答案,请问问自己,您所做的是否是该语言为您的用例提供的工具的最直接使用。
现在,对于完全不同的事情。。。
……比如在获取索引之前确认项目的存在。这种方法的好处是,函数总是返回一个索引列表——即使它是一个空列表。它也适用于字符串。
def indices(l, val):
"""Always returns a list containing the indices of val in the_list"""
retval = []
last = 0
while val in l[last:]:
i = l[last:].index(val)
retval.append(last + i)
last += i + 1
return retval
l = ['bar','foo','bar','baz','bar','bar']
q = 'bar'
print indices(l,q)
print indices(l,'bat')
print indices('abcdaababb','a')
粘贴到交互式python窗口时:
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def indices(the_list, val):
... """Always returns a list containing the indices of val in the_list"""
... retval = []
... last = 0
... while val in the_list[last:]:
... i = the_list[last:].index(val)
... retval.append(last + i)
... last += i + 1
... return retval
...
>>> l = ['bar','foo','bar','baz','bar','bar']
>>> q = 'bar'
>>> print indices(l,q)
[0, 2, 4, 5]
>>> print indices(l,'bat')
[]
>>> print indices('abcdaababb','a')
[0, 4, 5, 7]
>>>
使现代化
经过又一年的python开发,我对自己的原始答案感到有点尴尬,所以为了澄清事实,我当然可以使用上面的代码;然而,获得相同行为的更惯用方法是使用列表理解以及enumerate()函数。
类似于:
def indices(l, val):
"""Always returns a list containing the indices of val in the_list"""
return [index for index, value in enumerate(l) if value == val]
l = ['bar','foo','bar','baz','bar','bar']
q = 'bar'
print indices(l,q)
print indices(l,'bat')
print indices('abcdaababb','a')
当粘贴到交互式python窗口中时,会产生:
Python 2.7.14 |Anaconda, Inc.| (default, Dec 7 2017, 11:07:58)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def indices(l, val):
... """Always returns a list containing the indices of val in the_list"""
... return [index for index, value in enumerate(l) if value == val]
...
>>> l = ['bar','foo','bar','baz','bar','bar']
>>> q = 'bar'
>>> print indices(l,q)
[0, 2, 4, 5]
>>> print indices(l,'bat')
[]
>>> print indices('abcdaababb','a')
[0, 4, 5, 7]
>>>
现在,在回顾了这个问题和所有答案之后,我意识到这正是FMc在其先前的回答中所建议的。在我最初回答这个问题的时候,我甚至没有看到这个答案,因为我不理解它。
如果上面的一行代码对您仍然没有意义,我强烈建议您使用谷歌“python列表理解”,并花几分钟时间熟悉一下。它只是众多强大功能中的一个,让使用Python开发代码成为一件乐事。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录