我想了解內置sum()
功能,但是,在start
參數已經蒸發腦海裏的工作:蟒蛇和功能 - `start`參數說明需要
a=[[1, 20], [2, 3]] b=[[[[[[1], 2], 3], 4], 5], 6] >>> sum(b,a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list >>> sum(a,b) [[[[[[1], 2], 3], 4], 5], 6, 1, 20, 2, 3]
>>> a=[1,2] >>> b=[3,4] >>> sum(a,b) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list >>> sum(b,a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list
我只是傻眼了這一點,不知道發生了什麼事。以下是python文檔必須說的:http://docs.python.org/library/functions.html#sum。這並沒有給出「如果開始不是字符串而不是整數?」的解釋。
我只對累積和使用'start'參數,所以type只是'int'。我不認爲它是針對這種病理性病例的。:) – halex
正如文檔中所指出的那樣:將'sum'限制爲數字:-),您將會減少頭痛。如果您必須連接嵌套列表,請做一些顯式構造 - 無論如何,您將擁有更多可維護的代碼。 – jsbueno