Python中的列表推导,可计算列表的最小值和最大值 [英] List Comprehensions in Python to compute minimum and maximum values of a list

查看:59
本文介绍了Python中的列表推导,可计算列表的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来计算列表的最小值和最大值以节省内存效率

i have the following code to compute minimum and maximum values of a list in order to save memory efficiency

x_min = float('+inf')
x_max = float('-inf')

for p in points_in_list:
    x_min = min(x_min, p)
    x_max = max(x_max, p)

其中points_in_list是一个(大)数字列表.我想知道是否有一种方法可以使用List Comprehensions计算最小值和最大值并节省内存.

where points_in_list is a (large) list of numbers. I wish to know if there is a method to compute with a List Comprehensions the min and max value and saving the memory.

推荐答案

我是生成器和理解力的忠实拥护者,但是在这种情况下,它们似乎不是正确的方法,因为:

I'm a big fan of generators and comprehensions, but in this case it seems they are not the right way to go, because:

  1. 您要计算列表的 min max
  2. 您的清单很大

如果您只想计算 min max 之一,则可以使用min/max函数.但是,由于两者都需要,因此您必须在列表上循环两次以首先提取最小值,然后提取最大值.IE.像这样的东西:

If you wanted to calculate only one of the min or max, you could just use the min/max function on it. But since you want both, you would have to loop over the list twice to extract first the min and then the max. I.e. something like this:

x_min = min(points)
x_max = max(points)

让我们玩一些时机.首先在列表中同时调用min和max:

Let's play with some some timings. First calling both min and max on the list:

>>> import timeit
>>> def with_gens(l):
...     return min(l), max(l)
...
>>> timeit.timeit('with_gens(range(6000000))', 'from __main__ import with_gens', number=5)
1.7451060887015188

,现在使用您的代码仅循环一次:

and now looping only once, using you code:

>>> def with_loop2(l):
...     x_max = float('+inf')
...     x_min = float('-inf')
...     for el in l:
...         x_min = min(x_min, el)
...         x_max = max(x_max, el)
...     return x_min, x_max
...
>>> timeit.timeit('with_loop2(range(6000000))', 'from __main__ import with_loop2', number=5)
11.636076105071083

疯狂,是吗?

此方法完全没有内存问题.但是,它在每个循环中设置 x_max x_min ,这实际上是不必要的浪费:您只想在发现更大或更小的值时重置变量.我们可以轻松解决这个问题.

There is no memory problem at all with this approach. However, it sets the x_max and x_min in each loop, which is actually an unnecessary waste: you only want to reset the variable when you have found a bigger/smaller value. We can easily address this.

所以...让我们尝试仅循环一次,但要避免不必要的重置.

So... let's try looping only once, but avoiding unnecessary resettings.

>>> def with_loop(l):
...     x_min = float('-inf')
...     x_max = float('+inf')
...     for el in l:
...         if el < x_min:
...             x_min = el
...         elif el > x_max:
...             x_max = el
...     return x_min, x_max
...
>>> timeit.timeit('with_loop(range(6000000))', 'from __main__ import with_loop', number=5)
3.961046726963332

哦惊喜

虽然纸上只循环一次的算法看起来更高效,但它被 min max 的内部优化所击败.此外,在每个循环中和仅在必要时设置var之间的差异是巨大的.你永远不会停止学习.

It seems while the algorithm of looping only once is on the paper more efficient, it is beaten by the inner optimization of min and max. Moreover, the difference between setting the var in each loop and only when necessary is huge. You never stop learning.

这篇关于Python中的列表推导,可计算列表的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆