gpt4 book ai didi

Python递归函数缺少结果

转载 作者:行者123 更新时间:2023-11-28 16:30:49 26 4
gpt4 key购买 nike

来自Python recursively appending list function尝试以递归方式获取与文件结构关联的权限列表。

我有这个功能:

def get_child_perms(self, folder, request, perm_list):
# Folder contains other folders
if folder.get_children():
# For every sub-folder
return [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
return folder.has_read_permission(request)

这将返回除包含其他文件夹的文件夹之外的所有结果。

folder <- Missing (allowed)
subfolder <- Missing (restricted)
subsubfolder <- Get this (restricted)
files

函数的输出是[对,错,错]

另一种情况是,A = 允许,R = 限制

folder  A
subfolder A
subsubfolder R
files
files
subfolder R
files
subfolder A
subsubfolder A
files
files
subfolder A
files
files

输出将是[真,真,假,假,真,真,真]

最佳答案

基本问题发生,您只返回文件夹权限,当文件夹没有任何子项时,当它有子项时,您不包括folder.has_read_permission(request) 在您的返回结果中,这很可能导致您出现问题。你需要做 -

def get_child_perms(self, folder, request, perm_list):
# Folder contains other folders
if folder.get_children():
# For every sub-folder
return [folder.has_read_permission(request)] + [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
return [folder.has_read_permission(request)]

这应该导致(未测试)-

[folderperm [subfolderperm [subsubfolderperm]]

关于Python递归函数缺少结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103424/

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