python求一组数据的次最大值或次最小值

import numpy as np
a = np.array(
    [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
)
np.random.shuffle(a)  # 打乱顺序
a_max = np.partition(a, -1)[-1]  # 最大值:14
a_2nd_max = np.partition(a, -2)[-2]  # 次最大值:13
a_min = np.partition(a, 0)[0]  # 最小值:5
a_2nd_min = np.partition(a, 1)[1]  # 次最小值:6

see also
numpy官方文档:numpy.partition

猜你喜欢

转载自blog.csdn.net/shiyuzuxiaqianli/article/details/115148942