heapq.n原始序列中返回结果的最大索引

发布于 2024-09-07 19:50:19 字数 155 浏览 7 评论 0原文

如何返回可迭代的第 n 大项的原始列表中的索引

heapq.nlargest(2, [100, 2, 400, 500, 400])

output = [(3,500), (2, 400)]

这已经花费了我几个小时。我想不通。

How do I return the index in the original list of the nth largest items of an iterable

heapq.nlargest(2, [100, 2, 400, 500, 400])

output = [(3,500), (2, 400)]

This already cost me a couple hours. I can't figure it out.

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

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

发布评论

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

评论(2

七秒鱼° 2024-09-14 19:50:19
>>> seq = [100, 2, 400, 500, 400]
>>> heapq.nlargest(2, enumerate(seq), key=lambda x: x[1])
[(3, 500), (2, 400)]
>>> seq = [100, 2, 400, 500, 400]
>>> heapq.nlargest(2, enumerate(seq), key=lambda x: x[1])
[(3, 500), (2, 400)]
我做我的改变 2024-09-14 19:50:19

您可以将 list.indexmap,对于小型 n (注意 list.index 返回值为 x 的 first 项列表中的索引):

>>> iterable = [100, 2, 400, 500, 400]
>>> map(iterable.index, heapq.nlargest(2, iterable))
[3, 2]

要查看关联值...

>>> map(lambda n: (n, iterable.index(n)), heapq.nlargest(2, iterable))
[(500, 3), (400, 2)]

对于较大的 n 请参阅@SilentGhost 的帖子。


编辑:对一些解决方案进行基准测试:

#!/usr/bin/env python
import heapq
from timeit import Timer

seq = [100, 2, 400, 500, 400]

def a(seq):
    """returns [(3, 500), (2, 400)]"""
    return heapq.nlargest(2, enumerate(seq), key=lambda x: x[1])

def b(seq):
    """returns [3, 2]"""
    return map(seq.index, heapq.nlargest(2, seq))

def c(seq):
    """returns [(500, 3), (400, 2)]"""
    map(lambda n: (n, seq.index(n)), heapq.nlargest(2, seq))

if __name__ == '__main__':
    _a = Timer("a(seq)", "from __main__ import a, seq")
    _b = Timer("b(seq)", "from __main__ import b, seq")
    _c = Timer("c(seq)", "from __main__ import c, seq") 

    loops = 1000000

    print _a.timeit(number=loops)
    print _b.timeit(number=loops)
    print _c.timeit(number=loops)

    # Core i5, 2.4GHz, Python 2.6, Darwin
    # 8.92712688446
    # 5.64332985878
    # 6.50824809074

You can use list.index in combination with map, which is fast for small n (beware the list.index returns the index in the list of the first item whose value is x):

>>> iterable = [100, 2, 400, 500, 400]
>>> map(iterable.index, heapq.nlargest(2, iterable))
[3, 2]

To see the associated values ...

>>> map(lambda n: (n, iterable.index(n)), heapq.nlargest(2, iterable))
[(500, 3), (400, 2)]

For larger n see @SilentGhost's post.


Edit: Benchmarked some solution:

#!/usr/bin/env python
import heapq
from timeit import Timer

seq = [100, 2, 400, 500, 400]

def a(seq):
    """returns [(3, 500), (2, 400)]"""
    return heapq.nlargest(2, enumerate(seq), key=lambda x: x[1])

def b(seq):
    """returns [3, 2]"""
    return map(seq.index, heapq.nlargest(2, seq))

def c(seq):
    """returns [(500, 3), (400, 2)]"""
    map(lambda n: (n, seq.index(n)), heapq.nlargest(2, seq))

if __name__ == '__main__':
    _a = Timer("a(seq)", "from __main__ import a, seq")
    _b = Timer("b(seq)", "from __main__ import b, seq")
    _c = Timer("c(seq)", "from __main__ import c, seq") 

    loops = 1000000

    print _a.timeit(number=loops)
    print _b.timeit(number=loops)
    print _c.timeit(number=loops)

    # Core i5, 2.4GHz, Python 2.6, Darwin
    # 8.92712688446
    # 5.64332985878
    # 6.50824809074
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文