Python 如何根据正则表达式列表查找不匹配的元素?
我试图找出列表中不存在的特定元素 比如说,我有l1由正则表达式组成的一些模式Python 如何根据正则表达式列表查找不匹配的元素?,python,regex,Python,Regex,我试图找出列表中不存在的特定元素 比如说,我有l1由正则表达式组成的一些模式 l1 = ["file.log","sample.log","abc_log_(\d+)_(\d+)_(\d+)_test-analysis.log","abc_(\d+)_(\d+)_(\d+)_sample-analysis.log"] # l1 consisting regexes are of standard set of files. test1 = ["file.log","sample.log",
l1 = ["file.log","sample.log","abc_log_(\d+)_(\d+)_(\d+)_test-analysis.log","abc_(\d+)_(\d+)_(\d+)_sample-analysis.log"]
# l1 consisting regexes are of standard set of files.
test1 = ["file.log","sample.log","abc_log_123_12_12_test-analysis.log","abc_145_20_20_sample-analysis.log"]
假设test1是测试列表,它将与l1进行比较,以检查是否生成了所有文件。在这种情况下,所有文件都存在
类似地,test2=[file.log,abc_145_20_20_sample-analysis.log]
当test2与l1进行比较时,应通知不会生成sample.log和以test-analysis.log文件结尾的文件
How can this be done with minimum complexity ?
请查找下面的代码
import re
l1 = ["file.log","sample.log","abc_log_(\d+)_(\d+)_(\d+)_test-analysis.log","abc_(\d+)_(\d+)_(\d+)_sample-analysis.log"]
test1 = ["file.log","sample.log","abc_log_123_12_12_test-analysis.log","abc_145_20_20_sample-analysis.log"]
#test1 = ["file.log","abc_145_20_20_sample-analysis.log"]
for i in l1:
flag = ""
tmp = []
for j in test1:
if re.match("^"+str(i)+"$",j):
flag = "yes"
tmp.append(True)
print "File {} present".format(i)
break
if flag != "yes":
print "File not present : {}".format(i)
tmp.append(False)
另外,请建议是否有更好的方法。如果test1列表中的顺序不相关,您可以将函数与a一起使用,从而避免临时变量
for i in l1:
if any([re.match("^"+str(i)+"$",j) for j in test1]):
print "File {} present".format(i)
else:
print "File not present : {}".format(i)
要谈论优化,首先需要一些代码。到目前为止你写了什么?有什么问题吗?有多慢?@WiktorStribiżew抱歉,没有包含代码。现在添加,希望没问题。对于给定的示例,i的str函数不是必需的。如果某些文件名仅由数字组成,并且尚未表示为字符串,则可能会有所帮助。在l1中的这些元素中,我可以将这些分组替换为一些字符吗?例如,在替换abc_log_abc_abc_abc_abc_test-analysis.log之后,上面的字符串将是这样的,abc_DEF_GHI_JKL_sample-analysis.logi可以在普通字符串中执行,但是如何在regex中执行呢?假设该字符串包含不同的模式,如\d+或。*注释仅用于由数字组成的文件名。就像一个没有前缀、后缀或扩展名的文件123456。只要您没有从组成正则表达式的部分获取错误类型error:不支持+:'int'和'str'的操作数类型,就不需要替换任何数字,甚至不需要更改文件名。在if循环中,我可以获得j的精确匹配值吗?因为我们正在匹配它