简体   繁体   English

返回列表中大于某个值的项目列表

[英]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

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!!所以贾斯汀又赢了!!

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生成一个TrueFalse值列表,然后作为索引来选择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.

相关问题 如何返回其值大于特定数字的节点列表 - how to return a list of nodes whose value are greater than a specific number 如果列表中的最后n个元素大于一个数字,则返回列表中的剩余项 - Return remaining items in list, if last n elements in list greater than a number Python:如何在未排序的列表中查找大于某个数字的所有项(大数据集) - Python: How to find all the items greater than some number in an unsorted list (large data set) 查找大于某个值的嵌套列表的索引 - Finding index of nested list greater than a value 查找列表中的值是否大于其下方的项目 - Finding if a value in a list is greater than the item below it 给定一个整数列表,如果列表的长度大于 1,则返回 true - Given a list of integers, return true if the length of the list is greater than 1 检查我的词典中是否有任何字母的值大于1,如果有,则打印一些文本,如果没有转换为列表, - Checking if any letters in my ddict has a value greater than one, if so print some text, if not convert to a list 检查几个列表的长度是否大于某个常数 - Check that length of several list is greater than some constant 返回数字大于列表中前一个数字的次数 - Return amount of times a number is greater than previous number in a list 返回 Python 中列表中每个子列表的大于 k 的数字索引 - Return index of numbers greater than k for each of sublists in list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM