使用求和函数在列表中添加对象时出现不支持的操作数类型错误

unsupported operand type error when adding objects within list using sum function

我有一个 Serie class 实现来表示一系列数据及其标签。当我添加 Serie 对象和数字时,我得到了预期的输出。但是,当我对列表中的相同元素求和时,出现以下错误消息:

TypeError: unsupported operand type(s) for +: 'int' and 'Serie'

玩具示例代码

作为玩具示例代码来理解我们可以使用的问题:

import pandas as pd
import numpy as np

class Serie(object):

    def __str__(self):

        s = "> SERIE " + str(self.tag) + ": \n"
        s += str(self.data)
        return s

    def __init__(self, tag=None, data=pd.DataFrame()):
        """
        Creates object Serie

        @type tag: str
        @param tag: Tag for the Serie
        """
        self.tag = tag
        self.data = data

    def __add__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            tag = str(other) + "+" + self.tag
            serie = Serie(tag)
            serie.data = self.data + other
        else:
            try:
                tag = self.tag + "+" + other.tag
            except:
                print ("ERROR: You can't add to somehing that is not a Serie or a number." )
                return None
            serie = Serie(tag)
            serie.data = self.data + other.data
        return serie

s1 = Serie("fibonacci",pd.Series([1,1,2,3,5,8]))
s2 = Serie("2power",pd.Series(np.linspace(1,6,6)**2))
s3 = 10
sumSerie = s1+s2+s3
print sumSerie

这会按预期打印结果:

>>> 
> SERIE 10+fibonacci+2power: 
0    12.0
1    15.0
2    21.0
3    29.0
4    40.0
5    54.0
dtype: float64

使用列表中的对象总和时出错

但是当我 运行 以下几行时:

l = [s1,s2,s3]
sum(l)

我收到错误消息:

sum(l) TypeError: unsupported operand type(s) for +: 'int' and 'Serie'

当我 运行:

时显示相同的错误消息
l2 = [s1,s2]
sum(l2)

但在 l2 列表中没有 int 变量。

问题

为什么会显示此错误消息?这令人困惑,因为我能够对列表外的对象求和。

我可以做些什么来实现对列表中的对象求和吗?

编辑

按照评论中的建议,我添加了 __radd__ 以正确重载 add 方法。所以我将以下行添加到 Serie class:

def __radd__(self,other):
    return self.__add__(other)

然后求和。但不如预期。

如果我运行下面的代码:

>>> print sum(l)

我得到这个输出:

> SERIE 10+0+fibonacci+2power: 
0    12.0
1    15.0
2    21.0
3    29.0
4    40.0
5    54.0
dtype: float64

这与我的预期完全不同。标签中有一个额外的 +0。怎么会这样?但是,如果我使用我在回答 print np.array(l).sum() 中陈述的选项,结果是正确的。

编辑 2

正确重载 add 方法后,建议我使用以下方法按预期执行求和:

reduce(lambda a, b: a+b, l)

这种方法能够对列表使用 sum 函数并获得正确的结果。

pault in the comments in sum method "start defaults to 0" as detailed in sum function's documentation 所述。这就是之前将额外的 +0 添加到标签的原因。

总而言之,我相信我更愿意使用使用 numpy.sum 函数的选项:

np.array(l).sum()

不确定为什么不能使用求和函数。但是从列表中获得所需输出的解决方法是从列表中创建一个 numpy 数组并使用 numpy.sum 函数,如下所示:

np.array(l).sum()

这按预期给出了列表中对象的总和。