在 python 中跟踪索引的正确方法是什么?

发布于 2024-12-02 03:01:21 字数 160 浏览 1 评论 0原文

现在我正在循环中跟踪我的索引,就像这样

index = 0
for entry in longList:
    if entry == 'foo':
        print index
    index += 1

有更好的方法吗?

Right now I am tracking my index in side the loop like this

index = 0
for entry in longList:
    if entry == 'foo':
        print index
    index += 1

is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别忘他 2024-12-09 03:01:21
for index, entry in enumerate(longList):
    if entry == 'foo':
        print index
for index, entry in enumerate(longList):
    if entry == 'foo':
        print index
青春如此纠结 2024-12-09 03:01:21

使用 enumerate() 内置函数。

for index, entry in enumerate(longList):
    if entry == 'foo':
        print index

但是,在您的具体情况下,您可以简单地执行 index = longList.index("foo")

编辑:如果您想查找多个匹配项的索引,就像在纯 Python 中尽可能快,以下代码应该可以解决问题:

indices = tuple(index for index, element in enumerate(longList) if element=='foo')

Use the enumerate() built-in function.

for index, entry in enumerate(longList):
    if entry == 'foo':
        print index

However, in your specific case, you can simply do index = longList.index("foo")

EDIT: If you want to find the indices of multiple matches pretty much as fast as is possible in pure Python, the following code should do the trick:

indices = tuple(index for index, element in enumerate(longList) if element=='foo')
暗藏城府 2024-12-09 03:01:21

我喜欢列表理解:)

[index for (index,entry) in enumerate(longList) if entry == 'foo']

I like list comprehension :)

[index for (index,entry) in enumerate(longList) if entry == 'foo']
温柔一刀 2024-12-09 03:01:21

是的,最好的方法是这样做:

longList.index('foo')

Yes, the best way is to do this:

longList.index('foo')
靖瑶 2024-12-09 03:01:21

使用枚举会是一个更好的主意。

for ind,item in enumerate(longList):
    if item == 'foo':
        print ind

Using enumerate would be a better idea.

for ind,item in enumerate(longList):
    if item == 'foo':
        print ind
缱绻入梦 2024-12-09 03:01:21

如果您的列表确实很长并且是静态的,您应该考虑使用查找表(实际上,索引列表的字典,以条目作为键)。在第一次搜索后,它几乎会收回成本,因为您当前总是迭代所有元素。

from collections import defaultdict

# Create and fill the table (only once or whenever the list changes)
lookupTable = defaultdict(list)
for index, entry in enumerate(longList):
    lookupTable[entry].append(index)

# Search the list (as many times as you want)
indexes = lookupTable.get('foo')
# and you get either 'None' or a list of indexes '[1,10,20]'

If your list is really long and static, you should consider using a lookup table (actually, a dictionary of index lists with the entry as the key). It will almost pay for itself after the first search, since you currently always iterate over all the elements.

from collections import defaultdict

# Create and fill the table (only once or whenever the list changes)
lookupTable = defaultdict(list)
for index, entry in enumerate(longList):
    lookupTable[entry].append(index)

# Search the list (as many times as you want)
indexes = lookupTable.get('foo')
# and you get either 'None' or a list of indexes '[1,10,20]'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文