gpt4 book ai didi

python - 使用双递归从列表中删除 John Wick

转载 作者:太空宇宙 更新时间:2023-11-04 08:31:33 26 4
gpt4 key购买 nike

为了让我接管 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'], []], '')

关于python - 使用双递归从列表中删除 John Wick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com