[英]Return list of items in list greater than some value
I have the following list我有以下清单
j=[4,5,6,7,1,3,7,5]
What's the simplest way to return [5,5,6,7,7]
being the elements in j greater or equal to 5?返回
[5,5,6,7,7]
是 j 中大于或等于 5 的元素的最简单方法是什么?
You can use a list comprehension to filter it:您可以使用列表理解来过滤它:
j2 = [i for i in j if i >= 5]
If you actually want it sorted like your example was, you can use sorted
:如果你真的希望它像你的例子一样排序,你可以使用
sorted
:
j2 = sorted(i for i in j if i >= 5)
or call sort
on the final list:或在最终列表中调用
sort
:
j2 = [i for i in j if i >= 5]
j2.sort()
A list comprehension is a simple approach:列表理解是一种简单的方法:
j2 = [x for x in j if x >= 5]
Alternately, you can use filter
for the exact same result:或者,您可以使用
filter
获得完全相同的结果:
j2 = filter(lambda x: x >= 5, j)
Note that the original list j
is unmodified.请注意,原始列表
j
未修改。
您可以使用列表理解:
[x for x in j if x >= 5]
Use filter
(short version without doing a function with lambda
, using __le__
):使用
filter
(使用__le__
不使用lambda
函数的简短版本):
j2 = filter((5).__le__, j)
Example (python 3):示例(python 3):
>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> j2
<filter object at 0x000000955D16DC18>
>>> list(j2)
[5, 6, 7, 7, 5]
>>>
Example (python 2):示例(python 2):
>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> j2
[5, 6, 7, 7, 5]
>>>
Use __le__
i recommend this, it's very easy, __le__
is your friend使用
__le__
我推荐这个,这很容易, __le__
是你的朋友
If want to sort it to desired output (both versions):如果要将其排序为所需的输出(两个版本):
>>> j=[4,5,6,7,1,3,7,5]
>>> j2 = filter((5).__le__, j)
>>> sorted(j2)
[5, 5, 6, 7, 7]
>>>
Use sorted
使用
sorted
>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5]) # Michael Mrozek
1.4558496298222325
>>> timeit(lambda: filter(lambda x: x >= 5, j)) # Justin Ardini
0.693048732089828
>>> timeit(lambda: filter((5).__le__, j)) # Mine
0.714461565831428
>>>
So Justin wins!!所以贾斯汀赢了!!
With number=1
: number=1
:
>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5],number=1) # Michael Mrozek
1.642193421957927e-05
>>> timeit(lambda: filter(lambda x: x >= 5, j),number=1) # Justin Ardini
3.421236300482633e-06
>>> timeit(lambda: filter((5).__le__, j),number=1) # Mine
1.8474676011237534e-05
>>>
So Michael wins!!所以迈克尔赢了!!
>>> from timeit import timeit
>>> timeit(lambda: [i for i in j if i >= 5],number=10) # Michael Mrozek
4.721306089550126e-05
>>> timeit(lambda: filter(lambda x: x >= 5, j),number=10) # Justin Ardini
1.0947956184281793e-05
>>> timeit(lambda: filter((5).__le__, j),number=10) # Mine
1.5053439710754901e-05
>>>
So Justin wins again!!所以贾斯汀又赢了!!
In case you are considering using the numpy
module, it makes this task very simple, as requested:如果您正在考虑使用
numpy
模块,它会按照要求使此任务变得非常简单:
import numpy as np
j = np.array([4, 5, 6, 7, 1, 3, 7, 5])
j2 = np.sort(j[j >= 5])
The code inside of the brackets, j >= 5
, produces a list of True
or False
values, which then serve as indices to select the desired values in j
.括号内的代码
j >= 5
生成一个True
或False
值列表,然后作为索引来选择j
所需的值。 Finally, we sort with the sort
function built into numpy
.最后,我们使用
numpy
内置的sort
函数进行sort
。
Tested result (a numpy
array):测试结果(一个
numpy
数组):
array([5, 5, 6, 7, 7])
Since your desired output is sorted, you also need to sort it:由于您想要的输出已排序,因此您还需要对其进行排序:
>>> j=[4, 5, 6, 7, 1, 3, 7, 5]
>>> sorted(x for x in j if x >= 5)
[5, 5, 6, 7, 7]
There is another way,还有一个办法,
j3 = j2 > 4; print(j2[j3])
tested in 3.x在 3.x 中测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.