【20211214】【Python】查找数组中最值元素、中值元素及其索引

一、最大值

        1. 最大值元素

        np.max(lis)

        2. 最大值索引

        np.argmax(lis) 或 np.where(lis==np.max(lis))

二、最小值

        1. 最小值元素

        np.min(lis)

        2. 最小值索引

        np.argmin(lis) 或 np.where(lis==np.min(lis))

三、中值

        1. 中值元素

        np.median(lis) 

        2. 中值索引

        Python 没有直接 argmedian 的命令,但可以使用 np.argsort(lis) 得到排序后的数组元素对应的索引数组,那么中值对应的索引就是索引数组的中值,即可返回中值索引。

        indLis_sorted = np.argsort(lis)

        indMedian = indLis_sorted(len(lis)//2)

四、举个栗子~

import numpy as np

a = [0, 0, 1, 1, 4, 7, 6, -1]

print(a)

# 最大值
print(np.max(a))  # 最大值元素
print(np.argmax(a))  # 最大值索引
print(np.where(a==np.max(a)))  # 最大值索引

# 最小值
print(np.min(a))  # 最小值元素
print(np.argmin(a))  # 最小值索引
print(np.where(a==np.min(a)))  # 最小值索引

# 中值
print(np.median(a))  # 中值元素
a_sorted_ind = np.argsort(a)  # 数组a升序排序后对应的索引数组
indMedian = a_sorted_ind [len(a)//2]  # 中值索引
print(a_sorted_ind)  # 排序后的索引数组
print(indMedian)  # 中值索引

         (参考:python中找出numpy array数组的最值及其索引

        (参考:python基础 | python中找出numpy array数组的最值及其索引

        (参考:python对数组进行排序,并输出排序后对应的索引值方式

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Satisfying

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值