繁体   English   中英

如果我有多个最小数字并且想要两个索引,如何在python数组中找到最小数字的索引?

[英]How do I find the Index of the smallest number in an array in python if I have multiple smallest numbers and want both indexes?

我有一个数组,我想在其中找到最小元素的索引。 我尝试了以下方法:

distance = [2,3,2,5,4,7,6]

a = distance.index(min(distance))

这将返回0,这是第一个最小距离的索引。 但是,我想找到所有这样的实例0和2。如何在Python中做到这一点?

使用np.where获取与给定值匹配的所有索引:

import numpy as np

distance = np.array([2,3,2,5,4,7,6])

np.where(distance == np.min(distance))[0]

Out[1]: array([0, 2])

随着数组大小的增长,Numpy的性能优于其他方法:

TimeIt比较测试的结果,改编自下面的Yannic Hamann代码

                     Length of Array x 7
Method               1       10      20      50     100    1000
Sorted Enumerate     2.47  16.291  33.643                      
List Comprehension  1.058   4.745   8.843  24.792              
Numpy               5.212   5.562   5.931    6.22  6.441  6.055
Defaultdict         2.376   9.061  16.116  39.299              

时序结果图

如果条件成立,您可以枚举数组元素并提取其索引:

min_value = min(distance)
[i for i,n in enumerate(distance) if n==min_value]
#[0,2]

令人惊讶的是, numpy答案似乎是最慢的。

更新:取决于输入列表的大小。

import numpy as np
import timeit
from collections import defaultdict


def weird_function_so_bad_to_read(distance):
    se = sorted(enumerate(distance), key=lambda x: x[1])
    smallest_numb = se[0][1]  # careful exceptions when list is empty
    return [x for x in se if smallest_numb == x[1]]
    # t1 = 1.8322973089525476


def pythonic_way(distance):
    min_value = min(distance)
    return [i for i, n in enumerate(distance) if n == min_value]
    # t2 = 0.8458914929069579


def fastest_dont_even_have_to_measure(np_distance):
    # np_distance = np.array([2, 3, 2, 5, 4, 7, 6])
    min_v = np.min(np_distance)
    return np.where(np_distance == min_v)[0]
    # t3 = 4.247801031917334


def dd_answer_was_my_first_guess_too(distance):
    d = defaultdict(list)  # a dictionary where every value is a list by default

    for idx, num in enumerate(distance):
        d[num].append(idx)  # for each number append the value of the index

    return d.get(min(distance))
    # t4 = 1.8876687170704827


def wrapper(func, *args, **kwargs):
    def wrapped():
        return func(*args, **kwargs)
    return wrapped


distance = [2, 3, 2, 5, 4, 7, 6]

t1 = wrapper(weird_function_so_bad_to_read, distance)
t2 = wrapper(pythonic_way, distance)
t3 = wrapper(fastest_dont_even_have_to_measure, np.array(distance))
t4 = wrapper(dd_answer_was_my_first_guess_too, distance)

print(timeit.timeit(t1))
print(timeit.timeit(t2))
print(timeit.timeit(t3))
print(timeit.timeit(t4))

您还可以执行以下list comprehension

distance = [2,3,2,5,4,7,6]
min_distance = min(distance)
[index for index, val in enumerate(distance) if val == min_distance]
>>> [0, 2]

我们可以使用临时字典来存储列表的索引,然后仅获取与列表的距离的最小值。 我们还将在这里使用一个简单的for循环,以便您逐步了解正在发生的情况。

from collections import defaultdict

d = defaultdict(list) # a dictionary where every value is a list by default

for idx, num in enumerate(distance):
    d[num].append(idx) # for each number append the value of the index

d.get(min(distance)) # fetch the indices of the min number from our dict

[0, 2]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM