TypeError:在列表中使用'sum()'时,'NoneType'对象不可迭代

rha*_*ll1 0 python sum list nonetype

我正进入(状态

TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)

当我试图找到一个列表的总和.

出现问题的地方:

if(sum(self._candidates) + self._allCandidates[self._depth]._weight > 20):
    self._left = Node(self._candidates, self._depth + 1, self._allCandidates)
else:
    self._left = Node(self._candidates.append(self._allCandidates[self._depth]), self._depth + 1, self._allCandidates)
Run Code Online (Sandbox Code Playgroud)

节点定义:

def __init__(self, candidates = [], depth = -1, allCandidates = []):
        self._candidates = candidates
        self._depth = depth
        self._allCandidates = allCandidates
Run Code Online (Sandbox Code Playgroud)

感谢您对此事的任何帮助.

g.d*_*d.c 5

这是错的:

Node(self._candidates.append(self._allCandidates[self._depth])
Run Code Online (Sandbox Code Playgroud)

来自的返回值.appendNone错误.

  • 而且,你将使用`candidates = []`默认参数让自己遇到大麻烦.这是在函数定义时计算的,因此该类的所有实例将共享相同的列表(除非传入另一个列表).建议复制列表:`self._candidates = candidates [:]` (2认同)