我想编写一个返回大于或等于 x
这是我的代码:
def threshold(a,x):
b = a[x:]
return b
如果x是3,并且a是[1,2,3,4,5] , 该函数将返回 [3,4,5]
** 忘记提及值可能未排序。输入可以是 [1,3,2,5,4],如果 x 是 3,它必须返回 [3,4,5] **
请您参考如下方法:
使用生成器表达式和 sorted :
>>> def threshold(a, x):
... return sorted(item for item in a if item >= x)
...
>>> threshold([1,3,2,5,4], 3)
[3, 4, 5]
