Python 返回列表中大于某个值的项目列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4587915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return list of items in list greater than some value
提问by Carlton
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 的元素的最简单方法是什么?
采纳答案by Michael Mrozek
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 sorton the final list:
或致电sort最终名单:
j2 = [i for i in j if i >= 5]
j2.sort()
回答by sepp2k
You can use a list comprehension:
您可以使用列表理解:
[x for x in j if x >= 5]
回答by Justin Ardini
A list comprehension is a simple approach:
列表理解是一种简单的方法:
j2 = [x for x in j if x >= 5]
Alternately, you can use filterfor the exact same result:
或者,您可以使用filter完全相同的结果:
j2 = filter(lambda x: x >= 5, j)
Note that the original list jis unmodified.
请注意,原始列表j未修改。
回答by Lennart Regebro
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]
回答by U10-Forward
Use filter(short version without doing a function with lambda, using __le__):
使用filter(不使用lambda、 使用的功能的简短版本__le__):
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
Timings:
时间:
>>> 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!!
所以贾斯汀又赢了!!
回答by Harsha VK
There is another way,
还有一个办法,
j3 = j2 > 4; print(j2[j3])
tested in 3.x
在 3.x 中测试
回答by Santiago Ruiz-Valdepe?as
In case you are considering using the numpymodule, 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 Trueor Falsevalues, which then serve as indices to select the desired values in j. Finally, we sort with the sortfunction built into numpy.
方括号 内的代码j >= 5生成一个True或False值列表,然后这些值用作索引以在 中选择所需的值j。最后,我们使用sort内置于numpy.
Tested result (a numpyarray):
测试结果(numpy数组):
array([5, 5, 6, 7, 7])

