如何找到与正则表达式列表不匹配的元素?

时间:2019-02-23 15:07:16

标签: 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","abc_log_123_12_12_test-analysis.log","abc_145_20_20_sample-analysis.log"]

test1是测试列表,将其与l1进行比较以检查是否生成了所有文件。 In this case all files are present.

类似地,test2 = ["file.log","abc_145_20_20_sample-analysis.log"]test2l1进行比较时,应通知未生成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)

而且,请提出是否有更好的方法/方法。

1 个答案:

答案 0 :(得分:1)

如果test1列表中的顺序无关紧要,则可以将any()函数与list comprehension一起使用,从而避免使用临时变量。

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)