Return list of items in list greater than some value

前端 未结 7 1843
故里飘歌
故里飘歌 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: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.

提交回复
热议问题