python快速求一个数组的最大值/最小值及其索引(写的清晰,亲测可用)

2022-09-21 13:08:55

我直接说我的需求,我的需求是在一对数组中找到最大的数值和其索引,并输出。

导入operator模块:

result = [0, 0, 0, 2, 0, 2, 39, 0, 1, 0, 0, 0, 0, 161, 0, 0, 407, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 472, 0, 652, 0, 0, 367, 0, 0, 0, 844, 0, 20, 0, 0, 0, 0]

max_index, max_number = max(enumerate(result), key=operator.itemgetter(1))

max_index即为索引,max_number即为最大值

  • 作者:沈伟-测试前行者
  • 原文链接:https://blog.csdn.net/a136332462/article/details/107272995
    更新时间:2022-09-21 13:08:55