Return list of items in list greater than some value

前端 未结 7 1857
故里飘歌
故里飘歌 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()
    

提交回复
热议问题