在Python中修改递归函数之外的列表
存在结构不清晰的多维列表:在Python中修改递归函数之外的列表,python,list,function,recursion,hyperlink,Python,List,Function,Recursion,Hyperlink,存在结构不清晰的多维列表: a=[[['123', '456'], ['789', '1011']], [['1213', '1415']], [['1617', '1819']]] 还有一个递归函数,用于操作列表: def get_The_Group_And_Rewrite_It(workingList): if isinstance(workingList[0],str): doSomething(workingList) else: for
a=[[['123', '456'], ['789', '1011']], [['1213', '1415']], [['1617', '1819']]]
还有一个递归函数,用于操作列表:
def get_The_Group_And_Rewrite_It(workingList):
if isinstance(workingList[0],str):
doSomething(workingList)
else:
for i in workingList:
get_The_Group_And_Rewrite_It(i)
通过get_the_Group_和Rewrite_,它应该得到一个列表,例如,['123'、'456'],一旦得到它,doSomething函数应该在整个列表中用['abc']重写它
与其他格式列表相同[str,str,…]。最后我应该得到类似的东西
a=[[['abc'], ['abc']], [['abc']], [['abc']]]
我看到C++中使用**很容易,但是如何在Python中实现呢?< /p> 对于这种情况,可以使用切片赋值:
>>> a = [[['123', '456']]]
>>> x = a[0][0]
>>> x[:] = ['abc']
>>> a
[[['abc']]]
不要进行常规赋值,因为这样会丢失原始对象。这就是全部。
>>> def f(workingList):
... if isinstance(workingList[0],str):
... workingList[:] = ['abc']
... else:
... for i in workingList:
... f(i)
...
>>> a=[[['123', '456'], ['789', '1011']], [['1213', '1415']], [['1617', '1819']]]
>>> f(a)
>>> a
[[['abc'], ['abc']], [['abc']], [['abc']]]