递归和列表变异
Recursion and List Mutation
我限制自己只能使用递归来改变列表,这样做时我遇到了一些小麻烦。
def expand_strings(L,s,i):
if len(L) == 0:
return
else:
if len(L[0]) - 1 <= i:
L[0] += s
elif len(L[0]) - 1 > i:
new_string = L[0][:i] + s + L[0][i:]
L[0] = new_string
expand_strings(L[1:], s, i)
return
L:包含可能的1个或多个字符串的输入列表
s:我需要 "insert" 或 "append" 到列表中的字符串元素的额外字符串部分
i: 要插入或附加 s 的字符串的索引。
此函数的主要目标如下:
1. if the index i within the range 0 ~ len(string_element_in_list), then I insert my s starting from index i
2. if the index i is larger than what the current string length, then I do the append s.
我现在遇到的问题是:我注意到递归只会改变输入列表中的第一个元素,并且第一个元素之后的每个元素都不会受到改变的影响,我想它可能与我传递给递归的新输入列表有关,但我不知道为什么这不起作用。
感谢您提前提供帮助。 :)
问题出在递归调用expand_strings(L[1:], s, i)
。当您使用切片获取列表的一部分时,python 会创建该子列表的全新副本。因此,递归调用会创建列表的副本(第一个元素除外),并处理该副本。
解决此问题的一种方法是 return从您的方法修改列表:
def expand_strings(L,s,i):
if len(L) == 0:
return []
else:
if len(L[0]) - 1 <= i:
L[0] += s
elif len(L[0]) - 1 > i:
new_string = L[0][:i] + s + L[0][i:]
L[0] = new_string
return [L[0]] + expand_strings(L[1:], s, i)
如果您不想每次都创建子列表的副本(以及 return 修改后的列表),您可以在方法中再添加一个参数来指定第一个元素的位置修改。基本情况是起始索引等于列表的长度。
def expand_strings(L,s,i,start):
if start == len(L):
return
if len(L[start]) - 1 <= i:
L[start] += s
else:
L[start] = L[start][:i] + s + L[start][i:]
expand_strings(L, s, i, start + 1)
我限制自己只能使用递归来改变列表,这样做时我遇到了一些小麻烦。
def expand_strings(L,s,i):
if len(L) == 0:
return
else:
if len(L[0]) - 1 <= i:
L[0] += s
elif len(L[0]) - 1 > i:
new_string = L[0][:i] + s + L[0][i:]
L[0] = new_string
expand_strings(L[1:], s, i)
return
L:包含可能的1个或多个字符串的输入列表 s:我需要 "insert" 或 "append" 到列表中的字符串元素的额外字符串部分 i: 要插入或附加 s 的字符串的索引。
此函数的主要目标如下:
1. if the index i within the range 0 ~ len(string_element_in_list), then I insert my s starting from index i
2. if the index i is larger than what the current string length, then I do the append s.
我现在遇到的问题是:我注意到递归只会改变输入列表中的第一个元素,并且第一个元素之后的每个元素都不会受到改变的影响,我想它可能与我传递给递归的新输入列表有关,但我不知道为什么这不起作用。
感谢您提前提供帮助。 :)
问题出在递归调用expand_strings(L[1:], s, i)
。当您使用切片获取列表的一部分时,python 会创建该子列表的全新副本。因此,递归调用会创建列表的副本(第一个元素除外),并处理该副本。
解决此问题的一种方法是 return从您的方法修改列表:
def expand_strings(L,s,i):
if len(L) == 0:
return []
else:
if len(L[0]) - 1 <= i:
L[0] += s
elif len(L[0]) - 1 > i:
new_string = L[0][:i] + s + L[0][i:]
L[0] = new_string
return [L[0]] + expand_strings(L[1:], s, i)
如果您不想每次都创建子列表的副本(以及 return 修改后的列表),您可以在方法中再添加一个参数来指定第一个元素的位置修改。基本情况是起始索引等于列表的长度。
def expand_strings(L,s,i,start):
if start == len(L):
return
if len(L[start]) - 1 <= i:
L[start] += s
else:
L[start] = L[start][:i] + s + L[start][i:]
expand_strings(L, s, i, start + 1)