【发布时间】:2019-01-03 11:10:06
【问题描述】:
所以我的函数需要过滤一个列表,以便它只返回一个列表,该列表仅包含在将函数应用于它而不使用任何循环时返回正值的值。我的代码目前是:
def positive_places(f, xs):
"""takes a function f and list xs and returns
a list of the values of xs which satisfy
f>0"""
y = list(map(f, xs))
x = filter(lambda i: i > 0, y)
return x
这当前返回函数的所有正输出值的列表,但是我需要原始列表 xs 中的对应值。
提前感谢您的帮助!
【问题讨论】:
-
“但是我需要原始列表 xs 中的相应值”是什么意思?意思是,你能解释一下吗?
-
为什么不能使用 for 循环?列表组合是这里最干净的解决方案
标签: python python-3.x