确定列表中大于阈值的项目的最有效方法是什么?

What is the most efficient way to the determine items of a list which are greater than a threshold?

基本上,我想知道什么是 最有效的方法 来查找 python 列表中值大于 n 的元素。

我相信,最简单但效率不高的方法如下,

for i in range(len(theList)):
    if theList[i] > n:
        subList.append(theList[i])

此外,我们有如下单行for

(subList for subList in theList if sublist > n)

(以上语法如有错误请指正)

终于可以使用filter()功能了,用起来不是很爽,至少对我来说是这样

以上方法都是我所知道的方法。如果您知道更好的方法,请告诉我。否则,请说明在效率和运行-time.

的意义上哪个是最好的

列表理解版本:

sublist = [ i for i in the_list if i > n ]

生成器表达式:(如果列表很大)

sublist = ( i for i in the_list if i > n )

对此没有总是正确的答案,并且有一些关于处理列表时不同方法的速度的 SO 帖子,例如参见here, here or here.

最快的方法可能在很大程度上取决于您的列表。 也就是说,让我们来看看建议的方法有多快。

对于这样的简单比较,您可以使用 timeit:

1.案例:for 循环

for_case = """newList=[]
for x in theList:
    if x > n:
            newList.append(x)"""

2。案例:列表理解

list_comp = '[x for x in theList if x > n]'

3。案例:过滤器(有点不像)

filtering = 'list(filter(lambda x: x > n, theList))'

一些准备:

import timeit
si = 'theList=range(2000);n=1000;'  # using list(range(2000)) has no effect on the ranking

让我们看看:

timeit.timeit(si+list_comp, number=10000)
Out[21]: 1.3985847820003983
timeit.timeit(si+filtering, number=10000)
Out[22]: 3.315784254024038
timeit.timeit(si+for_case, number=10000)
Out[23]: 2.0093530920275953

所以,至少在我的机器上,列表理解把它拿走了,然后是 for 循环,至少在这种情况下,不受欢迎的 filter 确实是最慢的。