所以即時通訊嘗試創建一個遞歸函數,它需要列表中的每個項目並將其總結,現在我知道了一個簡單的內置函數sum(a),但我試圖使用這樣的嵌套列表下面,但我不斷收到錯誤。彙總嵌套列表值的函數?
def sumList():
list2 = [1, [2, 3,[4, 5, 6], 7, [8, [9, 10]], 11]]
newlist = []
lol = 0
for i in range (len(list2)):
if type(list2[i]) == type([]):
print list2[i], "here"
for i in range (len(list2[i])):
lol += (len(list2[i]))
newlist.append(i[:len(i)+1])
if len(list2)==0:
return None
else:
print list2[i]
lol+=list2[i]
print lol
sumList()
現在我知道我在我想象不需要程序實現了很多,但我 不斷收到錯誤
1
[2, 3, [4, 5, 6], 7, [8, [9, 10]], 11] here
TypeError: object of type 'int' has no len()
我只有1 – MrPorba 2014-12-03 03:56:07
我不能重複你的結果的輸出 - 仍出現66我。哪個版本的Python? – rchang 2014-12-03 03:57:46