python返回list最大值对应的index_python如何获得list或numpy数组中最大元素对应的索引...

获得list中最大元素的索引

aa = [1,2,3,4,5]

aa.index(max(aa))

相应的最小值使用

aa = [1,2,3,4,5]

aa.index(min(aa))

获得numpy数组中最大元素的索引

1.可以使用numpy的函数,argmax获得最大元素的索引,相应的获得最小值的话需要使用argmin。

aa = [1,2,3,4,5]

arr_aa = np.array(aa)

maxindex = np.argmax(arr_aa )

1.也可以将numpy转为list,然后使用list或者最大值索引的方法获得最大值。

aa = numpy.array([1,2,3,4,5])

先把aa转换为List,再求索引:

bb = aa.tolist()

bb.index(max(bb))

1.python 比较灵活,所以还可以有其他的方法,如使用where函数。

首先我们可以得到array在全局和每行每列的最大值(最小值同理)

>>> a = np.arange(9).reshape((3,3))

>>> a

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

>>> print(np.max(a)) #全局最大

8

>>> print(np.max(a,axis=0)) #每列最大

[6 7 8]

>>> print(np.max(a,axis=1)) #每行最大

[2 5 8]

然后用where得到最大值的索引,返回值中,前面的array对应行数,后者对应列数

>>> print(np.where(a==np.max(a)))

(array([2], dtype=int64), array([2], dtype=int64)) #表示最大值在第二行第二列

>>> print(np.where(a==np.max(a,axis=0)))

(array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64)) # 表示最大值分别在第二行第零列,第二行第一列,第二行第二列

如果array中有相同的最大值,where会将其位置全部给出

>>> a[1,0]=8

>>> a

array([[0, 1, 2],

[8, 4, 5],

[6, 7, 8]])

>>> print(np.where(a==np.max(a)))

(array([1, 2], dtype=int64), array([0, 2], dtype=int64))

到此这篇关于python如何获得list或numpy数组中最大元素对应的索引的文章就介绍到这了,更多相关python 获得list或numpy最大元素索引内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Python,可以使用不同的方法来找到数组的最大值索引。其一种方法是使用NumPy的max()和argmax()函数。max()函数用于找到数组的最大值,而argmax()函数用于找到数组中最大值索引。 使用max()函数,可以通过指定axis参数来确定是在整个数组中查找最大值还是在特定的轴上查找最大值。例如,使用np.max(a)可以找到数组a的全局最大值。而使用np.max(a, axis=0)可以按列查找每一列元素最大值返回一个大小为3的列表。同样,使用np.max(a, axis=1)可以按行查找每一行元素最大值。\[1\] 另一种方法是使用enumerate()函数和operator.itemgetter()方法。通过将数组元素与其索引一起枚举,然后使用min()和max()函数结合operator.itemgetter(1)来找到最小值和最大值索引。例如,使用min(enumerate(x), key=operator.itemgetter(1))可以找到数组x最小值的索引和值,而max(enumerate(x), key=operator.itemgetter(1))可以找到数组x最大值索引和值。\[2\] 最后,使用argmax()函数可以找到数组中最大值索引。可以通过指定axis参数来确定是在整个数组中查找最大值索引还是在特定的轴上查找最大值索引。如果不设置axis参数,argmax()函数会将数组展平后返回最大值索引。例如,使用np.argmax(x)可以找到数组x最大值索引,使用np.argmax(x, axis=0)可以按列查找每一列元素最大值索引,使用np.argmax(x, axis=1)可以按行查找每一行元素最大值索引。\[3\] #### 引用[.reference_title] - *1* [python 如何获得listnumpy数组中最大元素对应索引](https://blog.csdn.net/qq_45288176/article/details/125143506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python 获取数组的最大值、最小值及索引](https://blog.csdn.net/u014651560/article/details/117262360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【Numpy学习13】排序,搜索和计数](https://blog.csdn.net/Xiao_Spring/article/details/109402579)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值