部分应用字符串相等函数
Partially apply a string equality function
据我了解,我可以将字符串与 is
和 ==
进行比较。有什么方法可以部分应用这些功能吗?
例如:
xs = ["hello", "world"]
functools.filter(functools.partial(is, "hello"), xs)
给我:
functools.filter(functools.partial(is, "hello"), xs)
^
SyntaxError: invalid syntax
我不知道你为什么要在这里使用部分。直接将它写成函数要容易得多,例如使用 lambda:
functools.filter(lambda x: x == 'hello', xs)
您可以使用 operator.eq
:
import operator
import functools
xs = ["hello", "world"]
functools.filter(functools.partial(operator.eq, "hello"), xs)
产量
['hello']
operator.eq(a, b)
等同于 a == b
.
据我了解,我可以将字符串与 is
和 ==
进行比较。有什么方法可以部分应用这些功能吗?
例如:
xs = ["hello", "world"]
functools.filter(functools.partial(is, "hello"), xs)
给我:
functools.filter(functools.partial(is, "hello"), xs)
^
SyntaxError: invalid syntax
我不知道你为什么要在这里使用部分。直接将它写成函数要容易得多,例如使用 lambda:
functools.filter(lambda x: x == 'hello', xs)
您可以使用 operator.eq
:
import operator
import functools
xs = ["hello", "world"]
functools.filter(functools.partial(operator.eq, "hello"), xs)
产量
['hello']
operator.eq(a, b)
等同于 a == b
.