为了让我接管 Matrix,我首先需要从中移除 John Wick。他非常善于隐藏(很明显),所以他把自己分成了一个列表和列表中的列表。例如:
environment = ["a", [**"j"**, "e", **"o"**], [**"h"**, **"n"**, "s",
**"w"**, "o"], [**"i", "c", "k"**]]
target = "johnwick"
只允许双重递归,以便从 Matrix 的列表中删除 John Wick。他不属于这里。这是我到目前为止想出的:
def no_wick(environment):
target = "johnwick"
clean_matrix = []
if not environment:
return clean_matrix
elif isinstance(environment[0], list):
???????
elif environment[0] not in target:
return clean_matrix.append(environment[0]) + no_wick(environment[1:])
else:
return no_wick(environment[1:])
你能帮我把约翰威克从黑客帝国中移除,而我照顾托马斯 A. 安德森吗?
请您参考如下方法:
如果我理解你是正确的,这个例子可以帮助你:
In [1]: target = 'johnwick'
In [2]: environment = ['a', ['j', 'e', 'o'], ['h', 'n', 's', 'w', 'o'], ['i', 'c', 'k']]
In [3]: def no_wick(l, target):
...: clear = []
...: for x in l:
...: if isinstance(x, list):
...: x, target = no_wick(x, target)
...: clear.append(x)
...: else:
...: if target.startswith(x):
...: target = target[1:]
...: else:
...: clear.append(x)
...: return clear, target
In [4]: no_wick(environment, target)
Out[4]: (['a', ['e'], ['s', 'o'], []], '')
