python快速求一个数组的最大值/最小值及其索引(写的清晰,亲测可用)
我直接说我的需求,我的需求是在一对数组中找到最大的数值和其索引,并输出。
导入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即为最大值