Return list of items in list greater than some value

前端 未结 7 1802
故里飘歌
故里飘歌 2020-11-29 02:16

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 o

相关标签:
7条回答
  • 2020-11-29 02:39

    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:

    j2 = sorted(i for i in j if i >= 5)
    

    or call sort on the final list:

    j2 = [i for i in j if i >= 5]
    j2.sort()
    
    0 讨论(0)
  • 2020-11-29 02:39

    Use filter (short version without doing a function with lambda, using __le__):

    j2 = filter((5).__le__, j)
    

    Example (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):

    >>> 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

    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

    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:

    >>> 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!!

    0 讨论(0)
  • 2020-11-29 02:45

    There is another way,

    j3 = j2 > 4; print(j2[j3])
    

    tested in 3.x

    0 讨论(0)
  • 2020-11-29 02:53

    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:

    j2 = filter(lambda x: x >= 5, j)
    

    Note that the original list j is unmodified.

    0 讨论(0)
  • 2020-11-29 02:54

    In case you are considering using the numpy module, it makes this task very simple, as requested:

    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. Finally, we sort with the sort function built into numpy.

    Tested result (a numpy array):

    array([5, 5, 6, 7, 7])
    
    0 讨论(0)
  • 2020-11-29 02:55

    You can use a list comprehension:

    [x for x in j if x >= 5]
    
    0 讨论(0)
提交回复
热议问题