1
對於我的計算機科學課,我們正在學習遞歸,並且我很難理解它。我的任務之一是創建一個算法,返回與L完全相同的另一個列表,但除去e的所有元素。不過,我目前拿出一個接收類型錯誤:只能連接列表(不是「NoneType」)列表
TypeError: can only concatenate list (not "NoneType") to list
這是什麼意思,我怎麼能解決這個問題
def removeAll(e, L):
'''returns another list that is identical to L except all elements of e are removed'''
if L==[]:
return []
if L[0]!=e:
return [L[0]]+removeAll(e,L[1:])
當'L'既不是一個空表也沒有不等於'e'第一要素,這個功能並沒有指定返回值,因此收益'None'。 – TigerhawkT3
處理了「L [0] == e」的情況在哪裏? – shahkalpesh