Is there an accepted efficient way to find the range (ie. max value - min value) of a list of numbers in python? I have tried using a loop and I know I can use the min
and max
functions with subtraction. I am just wondering if there is some kind of built-in that is faster.
是否有一种可接受的有效方法来查找python中数字列表的范围(即最大值 - 最小值)?我尝试过使用循环,我知道我可以使用减法的最小和最大函数。我只是想知道是否有某种更快的内置。
3 个解决方案
#1
11
If you really need high performance, try Numpy. The function numpy.ptp
computes the range of values (i.e. max - min
) across an array.
如果你真的需要高性能,试试Numpy。函数numpy.ptp计算数组中值的范围(即max - min)。
#2
4
You're unlikely to find anything faster than the min
and max
functions.
你不可能找到比min和max函数更快的东西。
You could possibly code up a minmax
function which did a single pass to calculate the two values rather than two passes but you should benchmark this to ensure it's faster. It may not be if it's written in Python itself but a C routine added to Python may do it. Something like (pseudo-code, even though it looks like Python):
您可以编写一个minmax函数,该函数执行单次传递来计算两个值而不是两次传递,但您应该对此进行基准测试以确保它更快。它可能不是,如果它是用Python本身编写的,但是添加到Python的C例程可能会这样做。像(伪代码,即使它看起来像Python):
def minmax (arr):
if arr is empty:
return (None, None)
themin = arr[0]
themax = arr[0]
for each value in arr[1:]:
if value < themin:
themin = value
else:
if value > themax:
themax = value
return (themin, themax)
Another possibility is to interpose your own class around the array (this may not be possible if you want to work on real arrays directly). This would basically perform the following steps:
另一种可能性是在数组周围插入自己的类(如果你想直接在真实数组上工作,这可能是不可能的)。这基本上会执行以下步骤:
- mark the initial empty array clean.
- if adding the first element to an array, set
themin
andthemax
to that value. - if adding element to a non-empty array, set
themin
andthemax
depending on how the new value compares to them. - if deleting an element that is equal to
themin
orthemax
, mark the array dirty. - if requesting min and max from a clean array, return
themin
andthemax
. - if requesting min and max from a dirty array, calculate
themin
andthemax
using loop in above pseudo-code, then set array to be clean.
标记初始空数组清理。
如果将第一个元素添加到数组,请将min和themax设置为该值。
如果将元素添加到非空数组,请根据新值与它们的比较来设置min和max。
如果删除等于themin或themax的元素,则将数组标记为脏。
如果从干净的数组中请求min和max,则返回min和max。
如果从脏数组中请求min和max,则使用上面的伪代码中的循环计算min和max,然后将array设置为clean。
What this does is to cache the minimum and maximum values so that, at worst, you only need to do the big calculation infrequently (after deletion of an element which was either the minimum or maximum). All other requests use cached information.
这样做是为了缓存最小值和最大值,以便在最坏的情况下,您只需要不经常进行大计算(在删除最小值或最大值的元素之后)。所有其他请求都使用缓存信息。
In addition, the adding of elements keep themin
and themax
up to date without a big calculation.
此外,元素的添加使得min和themax保持最新,而无需大量计算。
And, possibly even better, you could maintain a dirty flag for each of themin
and themax
, so that dirtying one would still allow you to use the cached value of the nother.
而且,甚至可能更好,你可以为每个min和themax维护一个脏标志,这样弄脏一个仍然允许你使用标记的缓存值。
#3
3
If you use Numpy and you have an 1-D array (or can create one quickly from a list), then there's the function numpy.ptp()
:
如果您使用Numpy并且您拥有一维数组(或者可以从列表中快速创建一个),那么就是函数numpy.ptp():
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html
#1
11
If you really need high performance, try Numpy. The function numpy.ptp
computes the range of values (i.e. max - min
) across an array.
如果你真的需要高性能,试试Numpy。函数numpy.ptp计算数组中值的范围(即max - min)。
#2
4
You're unlikely to find anything faster than the min
and max
functions.
你不可能找到比min和max函数更快的东西。
You could possibly code up a minmax
function which did a single pass to calculate the two values rather than two passes but you should benchmark this to ensure it's faster. It may not be if it's written in Python itself but a C routine added to Python may do it. Something like (pseudo-code, even though it looks like Python):
您可以编写一个minmax函数,该函数执行单次传递来计算两个值而不是两次传递,但您应该对此进行基准测试以确保它更快。它可能不是,如果它是用Python本身编写的,但是添加到Python的C例程可能会这样做。像(伪代码,即使它看起来像Python):
def minmax (arr):
if arr is empty:
return (None, None)
themin = arr[0]
themax = arr[0]
for each value in arr[1:]:
if value < themin:
themin = value
else:
if value > themax:
themax = value
return (themin, themax)
Another possibility is to interpose your own class around the array (this may not be possible if you want to work on real arrays directly). This would basically perform the following steps:
另一种可能性是在数组周围插入自己的类(如果你想直接在真实数组上工作,这可能是不可能的)。这基本上会执行以下步骤:
- mark the initial empty array clean.
- if adding the first element to an array, set
themin
andthemax
to that value. - if adding element to a non-empty array, set
themin
andthemax
depending on how the new value compares to them. - if deleting an element that is equal to
themin
orthemax
, mark the array dirty. - if requesting min and max from a clean array, return
themin
andthemax
. - if requesting min and max from a dirty array, calculate
themin
andthemax
using loop in above pseudo-code, then set array to be clean.
标记初始空数组清理。
如果将第一个元素添加到数组,请将min和themax设置为该值。
如果将元素添加到非空数组,请根据新值与它们的比较来设置min和max。
如果删除等于themin或themax的元素,则将数组标记为脏。
如果从干净的数组中请求min和max,则返回min和max。
如果从脏数组中请求min和max,则使用上面的伪代码中的循环计算min和max,然后将array设置为clean。
What this does is to cache the minimum and maximum values so that, at worst, you only need to do the big calculation infrequently (after deletion of an element which was either the minimum or maximum). All other requests use cached information.
这样做是为了缓存最小值和最大值,以便在最坏的情况下,您只需要不经常进行大计算(在删除最小值或最大值的元素之后)。所有其他请求都使用缓存信息。
In addition, the adding of elements keep themin
and themax
up to date without a big calculation.
此外,元素的添加使得min和themax保持最新,而无需大量计算。
And, possibly even better, you could maintain a dirty flag for each of themin
and themax
, so that dirtying one would still allow you to use the cached value of the nother.
而且,甚至可能更好,你可以为每个min和themax维护一个脏标志,这样弄脏一个仍然允许你使用标记的缓存值。
#3
3
If you use Numpy and you have an 1-D array (or can create one quickly from a list), then there's the function numpy.ptp()
:
如果您使用Numpy并且您拥有一维数组(或者可以从列表中快速创建一个),那么就是函数numpy.ptp():
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html