检查字符串 includes/does 不包括来自不同列表的值
Check string includes/does not include values from distinct lists
listIncludedFolders = ["Criteria1"]
listExcludedFolders = ["Criteria2"]
for dirpath, dirnames, filenames in os.walk(root):
proceed = False
for each in listIncludedFolders:
if each in dirpath:
proceed = True
if proceed == True:
for each in listExcludedFolders:
if each in dirpath:
proceed = False
if proceed == True:
print(dirpath)
我正在尝试以更 pythonic 的方式实现以下代码。使用生成器,我可以设法基于单个列表的项目继续:
if any(dir in dirpath for dir in listIncludedFolders):
print(dirpath)
...但是我无法添加第二个比较。我在下面管理了一个附加条件,但我需要遍历附加条件列表:
if any(dir in dirpath for dir in listIncludedFolders if("Criteria2" not in dirpath)):
print(dirpath)
我怎样才能做到这一点 'cleanly'?
将两个条件与 and
运算符与另一个 any
调用组合:
if any(each in dirpath for each in listIncludedFolders) and \
not any(each in dirpath for each in listExcludedFolders):
print(dirpath)
或另一个 and
调用(条件被否定):
if any(each in dirpath for each in listIncludedFolders) and \
all(each not in dirpath for each in listExcludedFolders):
print(dirpath)
顺便说一句,(... for .. in .. if ..)
是 generator expression, not a list comrpehension。
这非常有效:
listIncludedFolders = ["Criteria1"]
listExcludedFolders = ["Criteria2"]
for dirpath, dirnames, filenames in os.walk(root):
if any(each in dirpath for each in listIncludedFolders) and \
not any(each in dirpath for each in listExcludedFolders):
print(dirpath)
您可以避免走进一开始就被排除在外的子树。此解决方案也比原始方法更可靠,假设测试子字符串以确定文件夹的包含和排除不是本意(您真的要排除名为 "Criteria2345" 的文件夹吗?)
for dirpath, dirnames, filenames in os.walk(root):
if set(dirpath.split(os.path.sep)) & set(listIncludedFolders):
print(dirpath)
for ex in [dnam for dnam in dirnames if dnam in listExcludedFolders]:
dirnames.remove(ex)
但是请注意,如果 root
在排除列表中,它将在此实现中被忽略。
listIncludedFolders = ["Criteria1"]
listExcludedFolders = ["Criteria2"]
for dirpath, dirnames, filenames in os.walk(root):
proceed = False
for each in listIncludedFolders:
if each in dirpath:
proceed = True
if proceed == True:
for each in listExcludedFolders:
if each in dirpath:
proceed = False
if proceed == True:
print(dirpath)
我正在尝试以更 pythonic 的方式实现以下代码。使用生成器,我可以设法基于单个列表的项目继续:
if any(dir in dirpath for dir in listIncludedFolders):
print(dirpath)
...但是我无法添加第二个比较。我在下面管理了一个附加条件,但我需要遍历附加条件列表:
if any(dir in dirpath for dir in listIncludedFolders if("Criteria2" not in dirpath)):
print(dirpath)
我怎样才能做到这一点 'cleanly'?
将两个条件与 and
运算符与另一个 any
调用组合:
if any(each in dirpath for each in listIncludedFolders) and \
not any(each in dirpath for each in listExcludedFolders):
print(dirpath)
或另一个 and
调用(条件被否定):
if any(each in dirpath for each in listIncludedFolders) and \
all(each not in dirpath for each in listExcludedFolders):
print(dirpath)
顺便说一句,(... for .. in .. if ..)
是 generator expression, not a list comrpehension。
这非常有效:
listIncludedFolders = ["Criteria1"]
listExcludedFolders = ["Criteria2"]
for dirpath, dirnames, filenames in os.walk(root):
if any(each in dirpath for each in listIncludedFolders) and \
not any(each in dirpath for each in listExcludedFolders):
print(dirpath)
您可以避免走进一开始就被排除在外的子树。此解决方案也比原始方法更可靠,假设测试子字符串以确定文件夹的包含和排除不是本意(您真的要排除名为 "Criteria2345" 的文件夹吗?)
for dirpath, dirnames, filenames in os.walk(root):
if set(dirpath.split(os.path.sep)) & set(listIncludedFolders):
print(dirpath)
for ex in [dnam for dnam in dirnames if dnam in listExcludedFolders]:
dirnames.remove(ex)
但是请注意,如果 root
在排除列表中,它将在此实现中被忽略。