[英]How to define own func argument inside itertools.accumulate?
The usage of itertools.accumulate is as follows: itertools.accumulate 的用法如下:
itertools.accumulate(iterable[, func, *, initial=None])
For example例如
list(itertools.accumulate([0, 7, 19, 13],operator.sub))
# [0, -7, -26, -39]
Where I used subtraction operator as func
argument.我在哪里使用减法运算符作为
func
参数。
How to define my own func
argument?如何定义我自己的
func
参数?
I want it similar to operator.sub
but instead of subtracting second item from first item I need to subtract first item from second item.我希望它与
operator.sub
类似,但不是从第一项中减去第二项,而是需要从第二项中减去第一项。
The output [0, -7, -26, -39]
was produced as follows:输出
[0, -7, -26, -39]
产生如下:
[0, (0) - (7), (0 - 7) - (19), ((0 - 7) - (19)) - (13)] == [0, -7, -26, -39]
I want it:我要它:
[0, (7) - (0), (19) - (7 - 0), (13) - ((19) - (7 - 0))] == [0, 7, 12, 1]`
It is same as with operator.sub
just the order of arguments inside subtraction is swapped.它与
operator.sub
相同,只是减法中的参数顺序被交换。 Or other way said - if operator.sub
is doing x1-x2
I want it to do x2-x1
at each iteration step.或者其他方式说 - 如果
operator.sub
正在执行x1-x2
我希望它在每个迭代步骤执行x2-x1
。
Everthing is an object in python, and can be passed around, including functions. Everthing 是 Python 中的一个对象,可以传递,包括函数。
func
can be any callable, which includes lambdas, functions, methods, and arbitrary objects whose class has a __call__
method. func
可以是任何可调用对象,包括 lambdas、函数、方法以及其类具有__call__
方法的任意对象。
Here are some examples of these options:以下是这些选项的一些示例:
A lambda
is a special one-expression function that is is very easy to define. lambda
是一种特殊的单表达式函数,很容易定义。 It's handy because you can use it anonymously inline:这很方便,因为您可以匿名内联使用它:
accumulate([0, 7, 19, 13], lambda a, b: b - a)
Since everything is an object, the lambda doesn't have to be anonymous:由于一切都是对象,因此 lambda 不必是匿名的:
func = lambda a, b: b - a accumulate([0, 7, 19, 13], func)
Functions are probably the most ubiquitous callable.函数可能是最普遍的可调用函数。 You make a function with the
def
statement:你用
def
语句创建一个函数:
def func(a, b): return b - a accumulate([0, 7, 19, 13], func)
Notice that this looks a lot like the lambda
when you assign it to a name.请注意,当您将其分配给名称时,这看起来很像
lambda
。 Lambdas and functions that return a single expression behave almost exactly the same in most regards.返回单个表达式的 Lambda 和函数在大多数方面的行为几乎完全相同。
You can use a built-in function as well:您也可以使用内置函数:
accumulate([0, 7, 19, 13], operator.rsub)
Even most class methods exist as functions in the class object.甚至大多数类方法都作为类对象中的函数存在。 If you have a uniform list, eg, all
int
s, you can take advantage of this:如果你有一个统一的列表,例如,所有的
int
s,你可以利用这个:
accumulate([0, 7, 19, 13], int.__rsub__)
This works because int.__rsub__
is a function accepting two arguments, which lives in the class int
.这是有效的,因为
int.__rsub__
是一个接受两个参数的函数,它位于类int
。 When you do (3).__rsub__(4)
, you bind that function to an instance, which turns it into a method with just one explicit argument.当你执行
(3).__rsub__(4)
,你将该函数绑定到一个实例,这将它变成一个只有一个显式参数的方法。
A more esoteric type of callable is an instance of a class with a __call__
method:一种更深奥的可调用类型是具有
__call__
方法的类的实例:
class Subtractor: def __call__(self, a, b): return b - a accumulate([0, 7, 19, 13], Subtractor())
Notice the parentheses in Subtractor()
.注意
Subtractor()
的括号。 They create a new instance, which is callable because the class defines __call__
.他们创建了一个新实例,该实例是可调用的,因为该类定义了
__call__
。
A special case of function-like objects are staticmethod
and classmethod
objects.类函数对象的一个特例是
staticmethod
和classmethod
对象。 They are not strictly functions, but wrap function objects inside a class.它们不是严格意义上的函数,而是将函数对象包装在一个类中。 If you have a utility class, the
@staticmethod
decorator could be applied to a function that does not rely on the class or instance state:如果您有一个实用程序类,
@staticmethod
装饰器可以应用于不依赖于类或实例状态的函数:
class Subtractor: @staticmethod def func1(a, b): return b - a @classmethod def func2(cls, a, b): return b - a accumulate([0, 7, 19, 13], Subtractor.func1) accumulate([0, 7, 19, 13], Subtractor().func2)
func1
behaves like a normal function. func1
行为就像一个普通函数。 In fact, it won't bind to an instance at all: you can also do accumulate([0, 7, 19, 13], Subtractor().func1)
.事实上,它根本不会绑定到一个实例:你也可以做
accumulate([0, 7, 19, 13], Subtractor().func1)
。 func2
, however, must be bound, but to the class and not the instance.但是,
func2
必须绑定到类而不是实例。 You could call accumulate([0, 7, 19, 13], Subtractor.func2)
as well.你也可以调用
accumulate([0, 7, 19, 13], Subtractor.func2)
。
Create a function with two arguments, that returns the result of subtracting the first from the second:创建一个带有两个参数的函数,返回从第二个参数中减去第一个参数的结果:
from itertools import accumulate
def my_sub(a, b):
return b - a
print(list(accumulate([0, 7, 19, 13], my_sub)))
Output:输出:
[0, 7, 12, 1]
Or you could use a lambda
:或者你可以使用
lambda
:
list(accumulate([0, 7, 19, 13], lambda a, b : b - a))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.