Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';非类型';对象在使用';sum()';名单上_Python_List_Sum_Nonetype - Fatal编程技术网

Python 类型错误:';非类型';对象在使用';sum()';名单上

Python 类型错误:';非类型';对象在使用';sum()';名单上,python,list,sum,nonetype,Python,List,Sum,Nonetype,我越来越 TypeError: 'NoneType' object is not iterable 当我试图找到一个列表的总和时 出现问题时: if(sum(self._candidates) + self._allCandidates[self._depth]._weight > 20): self._left = Node(self._candidates, self._depth + 1, self._allCandidates) else: self._left

我越来越

TypeError: 'NoneType' object is not iterable
当我试图找到一个列表的总和时

出现问题时:

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)
节点定义:

def __init__(self, candidates = [], depth = -1, allCandidates = []):
        self._candidates = candidates
        self._depth = depth
        self._allCandidates = allCandidates
感谢您对此事的帮助。

这是错误的:

Node(self._candidates.append(self._allCandidates[self._depth])
.append
的返回值为
None
,因此出现错误。

这是错误的:

Node(self._candidates.append(self._allCandidates[self._depth])

.append
的返回值是
None
,因此出现了错误。

此外,使用
candidates=[]
默认参数会给您带来很大的麻烦。这是在函数定义时计算的,因此该类的所有实例将共享同一个列表(除非传入另一个列表)。建议复制列表:
self.\u candidates=candidates[:]
另外,您将在使用
candidates=[]
默认参数时遇到大麻烦。这是在函数定义时计算的,因此该类的所有实例将共享同一个列表(除非传入另一个列表)。建议复制列表:
self.\u候选者=候选者[:]