我想在python中遞歸地將一個列表復制到另一個列表。為此,我將字符串作為輸入,定義了一個空列表并將它們作為列表發(fā)送到遞歸函數(shù),以便我可以將列表復制到另一個列表。但是,錯誤顯示“NoneType”對象沒有屬性“append”。我錯過了什么?我在 main() 中定義了“S”作為列表。如果有其他遞歸方法,歡迎使用。顯示錯誤:line 35, in string_copyreturn string_copy(k,s.append(k[i]),i+1)AttributeError: 'NoneType' object has no attribute 'append'代碼是:def string_copy(k,s,i): if (i == len(k)): return s; else: return string_copy(k,s.append(k[i]),i+1)def main(): print("enter the string you want to copy:"); k = input(); s = [None]; i = 0; print("the type of k and s is:", type(k),type(s)); res = string_copy(list(k),list(s),i); print("the desired results are:","\n", res);if __name__ == "__main__": main() `
在python中遞歸地將一個列表復制到另一個列表
ibeautiful
2021-06-04 15:13:24