Python equivalent of Ruby's .select

后端 未结 3 1734
执念已碎
执念已碎 2021-01-12 13:34

I have an list/array, lets call it x, and I want to create a new list/array, lets call this one z, out of elements from x that match a

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 14:28

    One option is to use list comprehension:

    >>> [a for a in x if a < 5]
    [1, 2, 3, 4]
    

提交回复
热议问题