从列表中过滤 - Python

use*_*517 0 python arrays list filter

我想知道是否有人可以帮我解决作业问题.

编写一个函数func(a,x),它接受一个数组,a,x都是数字,并返回一个只包含大于或等于x的值的数组

我有

def threshold(a,x):
    for i in a:
        if i>x: print i
Run Code Online (Sandbox Code Playgroud)

但这是错误的方法,因为我没有将它作为数组返回.有人能暗示我正确的方向.非常感谢提前

Ash*_*ary 6

使用内置函数filter():

In [59]: lis=[1,2,3,4,5,6,7]
In [61]: filter(lambda x:x>=3,lis)  #return only those values which are >=3
Out[61]: [3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)