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:x
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]

过滤器(lambda x:x
f
返回布尔值。
g
h
返回函数。为什么它们是等价的?条件语句中的函数总是被认为是真的。
f
返回布尔值。
g
h
返回函数。为什么它们是等价的?条件语句中的函数总是如此这被认为是真的。