Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中从数组的特定部分获取最大值_Python_Arrays_Numpy_Max - Fatal编程技术网

如何在python中从数组的特定部分获取最大值

如何在python中从数组的特定部分获取最大值,python,arrays,numpy,max,Python,Arrays,Numpy,Max,我有一个特定的场景,我需要扫描数组的特定部分,查找该部分的最大值,并返回该值相对于整个数组的位置。 比如说 searchArray = [10,20,30,40,50,60,100,80,90,110] 我想扫描第3部分到第8部分中的最大值,(40,50,60100,80,90) 然后返回该值的位置 所以在这种情况下,最大值是100,位置是6 有没有一种方法可以单独使用python或在numpy的帮助下实现这一点?我会这样做: sliced = searchArray[3:9] m = ma

我有一个特定的场景,我需要扫描数组的特定部分,查找该部分的最大值,并返回该值相对于整个数组的位置。 比如说

 searchArray = [10,20,30,40,50,60,100,80,90,110]
我想扫描第3部分到第8部分中的最大值,(40,50,60100,80,90)

然后返回该值的位置

所以在这种情况下,最大值是100,位置是6

有没有一种方法可以单独使用python或在numpy的帮助下实现这一点?我会这样做:

sliced = searchArray[3:9]
m = max(sliced)
pos = sliced.index(m) + 3

我在位置上添加了一个偏移量
3
,以提供未修改列表中的真实索引。

我猜这就是您想要的

maxVal = max(searchArray[3:8]) // to get max element

position = searchArray.index(max(ary[3:8])) //to get the position of the index

首先对列表进行切片,然后在max函数中使用索引:

searchArray = [10,20,30,40,50,60,100,80,90,110]
slicedArray = searchArray[3:9]
print slicedArray.index(max(slicedArray))+3

这将返回切片数组的索引,加上添加的beginSlice

尝试此操作…假设您希望整个列表中的max索引-

import numpy as np

searchArray = [10,20,30,40,50,60,100,80,90,110]

start_index = 3
end_index = 8

print (np.argmax(searchArray[start_index:end_index+1]) + start_index)

使用
enumerate
获取保存索引和值的元组枚举列表(实际上它是一个生成器,这意味着它总是只需要一个条目的内存,而不需要整个列表的内存),然后使用
max
和自定义比较器函数查找最大值:

searchArray = [10,20,30,40,50,60,100,80,90,110]
lower_bound = 3  # the lower bound is inclusive, i.e. element 3 is the first checked one
upper_bound = 9  # the upper bound is exclusive, i.e. element 8 (9-1) is the last checked one

max_index, max_value = max(enumerate(searchArray[lower_bound:upper_bound], lower_bound),
                           key=lambda x: x[1])

print max_index, max_value
# output: 6 100

使用
itemgetter

pos = max(enumerate(searcharray[3:9], 3), key=itemgetter(1))[0]

没有回答这个问题,他们需要indexposition=searchArray.index(max(ary[3:8]),这里3是下限,8是上限。这没有提供正确的索引值。它应该是原始列表中的索引。我在最后一行说过,但我现在更改了代码。没问题!如果你能接受这个答案,那就太完美了。但是print`print searchArray.index(max(slicedArray))呢。这也很好,不是吗?首先调用
max
,它遍历整个列表以找到最高值,然后调用
index
,它再次遍历列表的一半以找到该元素的位置是多余的,如果性能有问题,应该避免使用。使用
枚举
生成器查看我的答案,以在搜索最大值之前获取索引,并避免对列表进行不必要的第二次迭代。非常感谢。但在这种情况下,使用切片器是有效的think@D_Wills这种方法也使用切片(
[下界:上界]
)-它只是避免了在列表中再次搜索由
max
返回的项以查找其索引。相反,我使用
enumerate
动态生成索引和值的元组,并让
max
处理它们。这样它只执行一次迭代,直接返回索引和值。我想你的意思是
max
和最大值。我喜欢
枚举(…,下界)
以及其他微妙的问题。。。最好的answer@ByteCommander非常感谢。但是当你把上限设为9时,它也应该扫描位置9。此代码不扫描最后一个位置。那么答案应该是位置9值110这是唯一可以接受的答案question@EelcoHoogendoorn你为什么这么想?Numpy在非常大的列表上甚至没有明显的更快,那么在您看来,这个解决方案比其他解决方案有什么优势呢?请解释一下。