Python lambdas的绑定 def(x): 返回x
此处Python lambdas的绑定 def(x): 返回x,python,Python,此处g和h返回函数对象,但filter等待返回布尔或的函数。使用f时,对于预期的输出是正确的,但是对于g和h,条件总是true,因为bool(函数对象)总是true请参见,python函数是可调用的对象 采取: 要调用g执行以下操作: [0,1,2,3,4] [0,1,2,3,4] 做: 输出: ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print([x for x in ls if f(x)]) # list comprehension # is equiva
g
和h
返回函数对象,但filter
等待返回布尔或的函数。使用f
时,对于预期的输出是正确的,但是对于g
和h
,条件总是true
,因为bool(函数对象)
总是true
请参见,python函数是可调用的对象
采取:
要调用g
执行以下操作:
[0,1,2,3,4]
[0,1,2,3,4]
做:
输出:
ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print([x for x in ls if f(x)]) # list comprehension
# is equivalent to filter(lambda x: x < 5, ls) iterator
# To get list :
# [x for x in filter(lambda x: x < 5, ls)]
# is equivalent to list(filter(lambda x: x < 5, ls))
print([x for x in ls if g(x)()])
print([x for x in ls if h(x)(x)])
# or print([x for x in ls if h(x)()])
要调用h
执行以下操作:
[0,1,2,3,4]
[0,1,2,3,4]
或
做:
输出:
ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print([x for x in ls if f(x)]) # list comprehension
# is equivalent to filter(lambda x: x < 5, ls) iterator
# To get list :
# [x for x in filter(lambda x: x < 5, ls)]
# is equivalent to list(filter(lambda x: x < 5, ls))
print([x for x in ls if g(x)()])
print([x for x in ls if h(x)(x)])
# or print([x for x in ls if h(x)()])
请参阅使用过滤器的步骤:
从iterable的那些元素构造一个列表,对于这些元素,函数返回true。iterable可以是序列、支持迭代的容器或迭代器。如果iterable是字符串或元组,则结果也具有该类型;否则它总是一个列表。如果function为None,则假定identity函数,即删除iterable中所有为false的元素
请注意,过滤器(函数,iterable)
相当于[函数中的项对应iterable if函数(item)]
如果函数不是None
和[函数中的项对应iterable if item]
如果函数是None
请参见使用lambda表达式的步骤:
lambda参数:表达式
带有:
[0,1,2,3,4]
def(参数):
返回表达式
此处g
和h
返回函数对象,但filter
等待返回布尔或的函数。使用f
时,对于预期的输出是正确的,但是对于g
和h
,条件总是true
,因为bool(函数对象)
总是true
请参见,python函数是可调用的对象
采取:
要调用g
执行以下操作:
[0,1,2,3,4]
[0,1,2,3,4]
做:
输出:
ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print([x for x in ls if f(x)]) # list comprehension
# is equivalent to filter(lambda x: x < 5, ls) iterator
# To get list :
# [x for x in filter(lambda x: x < 5, ls)]
# is equivalent to list(filter(lambda x: x < 5, ls))
print([x for x in ls if g(x)()])
print([x for x in ls if h(x)(x)])
# or print([x for x in ls if h(x)()])
要调用h
执行以下操作:
[0,1,2,3,4]
[0,1,2,3,4]
或
做:
输出:
ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print([x for x in ls if f(x)]) # list comprehension
# is equivalent to filter(lambda x: x < 5, ls) iterator
# To get list :
# [x for x in filter(lambda x: x < 5, ls)]
# is equivalent to list(filter(lambda x: x < 5, ls))
print([x for x in ls if g(x)()])
print([x for x in ls if h(x)(x)])
# or print([x for x in ls if h(x)()])
请参阅使用过滤器的步骤:
从iterable的那些元素构造一个列表,对于这些元素,函数返回true。iterable可以是序列、支持迭代的容器或迭代器。如果iterable是字符串或元组,则结果也具有该类型;否则它总是一个列表。如果function为None,则假定identity函数,即删除iterable中所有为false的元素
请注意,过滤器(函数,iterable)
相当于[函数中的项对应iterable if函数(item)]
如果函数不是None
和[函数中的项对应iterable if item]
如果函数是None
请参见使用lambda表达式的步骤:
lambda参数:表达式
带有:
[0,1,2,3,4]
def(参数):
返回表达式
g(x)
和h(x)
返回lambda
函数本身(
),但不执行它
例如:g(3)(
,它将返回所需的值
因此,运行g(x)(
或h(x)(
将起作用:
def <lambda>(arguments):
return expression
当仅运行g(x)
或h(x)
时,每个值将返回lambda
函数本身,这相当于filter
函数中的True
语句,因此不过滤任何值
当然,在这种情况下,您可以运行:
[x for x in filter(lambda x: g(x)(), ls)] # [0,1,2,3,4]
[x for x in filter(lambda x: h(x)(), ls)] # [0,1,2,3,4]
filter(lambda x:xg(x)
和h(x)
返回lambda
函数本身(
),但不执行它
例如:g(3)(
,它将返回所需的值
因此,运行g(x)(
或h(x)(
将起作用:
def <lambda>(arguments):
return expression
当仅运行g(x)
或h(x)
时,每个值将返回lambda
函数本身,这相当于filter
函数中的True
语句,因此不过滤任何值
当然,在这种情况下,您可以运行:
[x for x in filter(lambda x: g(x)(), ls)] # [0,1,2,3,4]
[x for x in filter(lambda x: h(x)(), ls)] # [0,1,2,3,4]
过滤器(lambda x:xf
返回布尔值。g
和h
返回函数。为什么它们是等价的?条件语句中的函数总是被认为是真的。f
返回布尔值。g
和h
返回函数。为什么它们是等价的?条件语句中的函数总是如此这被认为是真的。