在我的代码中,我的返回值附加到列表中。将所有项目附加到单个列表的方式是什么?现在,每个堆栈都会创建一个列表并返回该列表。
def palindrome_partition(string):
if len(string) <=2:
rev = string[::-1]
if string == rev:
return string
else:
return
pal = []
b = string[1:-1]
if len(b) <=1:
return
a = b[::-1]
res = palindrome_partition(b)
if res is not None:
pal.append(res)
if a == b:
pal.append(b)
return pal
结果为[[[],]]
所需结果[ , , ]
答案 0 :(得分:2)
IIUC,替换
pal.append(res)
使用
pal += res