正则表达式列表比较 Python

huangapple go评论76阅读模式
英文:

Regex list comparison Python

问题

如何基于列表中的模式比较两个Python列表?

案例1:

List_1 = ['aaa_123.csv', 'bbb_456.csv', 'ccc_789.csv', 'aaa_321.csv']

List_2 = ['aaa*.csv', 'bbb*.csv', 'ccc*.csv']

预期输出: List_2中的所有元素在List_1中都有匹配的模式。

案例2:

List_1 = ['aaa_123.csv', 'bbb_456.csv', 'aaa_321.csv']

List_2 = ['aaa*.csv', 'bbb*.csv', 'ccc*.csv', 'ddd*.csv']

预期输出: List_2的模式与List_1不完全匹配。找不到元素'ccc*csv'和'ddd.csv'的匹配。

英文:

How to compare two lists in python based on pattern from a list?

Case 1:

List_1 = ['aaa_123.csv','bbb_456.csv','ccc_789.csv','aaa_321.csv']

List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv']

Expected output: All the elements in List_2 have matching pattern in List_1.

Case 2:

List_1 = ['aaa_123.csv','bbb_456.csv','aaa_321.csv']

List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv','ddd*.csv']

Expected output: List_2 pattern is not fully matching with list_1. Match for the elements 'ccc*csv' & 'ddd.csv' is not found.

答案1

得分: 1

Python的`fnmatch`模块就是为了实现这个目的而设计的记住你那里的模式不是正则表达式它们是shell中的glob模式`fnmatch`就像是字符串的`glob`:

import fnmatch

def check( names, patterns ):
    return all(fnmatch.filter(names,p) for p in patterns)

List_1 = ['aaa_123.csv','bbb_456.csv','ccc_789.csv','aaa_321.csv']
List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv']
print(check(List_1,List_2))

List_1 = ['aaa_123.csv','bbb_456.csv','aaa_321.csv']
List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv','ddd*.csv']
print(check(List_1,List_2))

输出:

True
False
英文:

The Python fnmatch module was designed for exactly this purpose. Remember, what you have there are not regular expressions. They are shell "glob" patterns, and fnmatch is like glob for strings:

import fnmatch

def check( names, patterns ):
    return all(fnmatch.filter(names,p) for p in patterns)

List_1 = ['aaa_123.csv','bbb_456.csv','ccc_789.csv','aaa_321.csv']
List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv']
print(check(List_1,List_2))

List_1 = ['aaa_123.csv','bbb_456.csv','aaa_321.csv']
List_2 = ['aaa*.csv','bbb*.csv', 'ccc*.csv','ddd*.csv']
print(check(List_1,List_2))

Output:

True
False

答案2

得分: 0

以下是您要翻译的内容:

以下的代码也应该可以工作。

import re

def compare_lists(list1, list2):
    return all(
        any(re.match(pattern.replace(""*"", ".*""), element) for element in list1)
        for pattern in list2
    )

我们使用了嵌套的列表推导来检查list2中的任何模式是否与list1中的任何元素匹配,使用正则表达式。我们使用replace()方法将每个模式中的替换为.,然后使用另一个生成器表达式和any()函数来检查生成的正则表达式是否与list1中的任何元素匹配。然后,我们使用all()函数来检查list2中的所有模式是否至少与list1中的一个元素匹配。

以下是如何使用这个函数与您提供的两个测试案例:

# 使用提供的示例测试函数
list1 = ["aaa_123.csv", "bbb_456.csv", "ccc_789.csv", "aaa_321.csv"]
list2 = ["aaa*.csv", "bbb*.csv", "ccc*.csv"]
result = compare_lists(list1, list2)
print(result)  # 应该打印True

list1 = ["aaa_123.csv", "bbb_456.csv", "aaa_321.csv"]
list2 = ["aaa*.csv", "bbb*.csv", "ccc*.csv", "ddd*.csv"]
result = compare_lists(list1, list2)
print(result)  # 应该打印False

结果:

True
False

希望这有助于您达到您想要的目标!

英文:

The following code should also work.

import re

def compare_lists(list1, list2):
    return all(
        any(re.match(pattern.replace("*", ".*"), element) for element in list1)
        for pattern in list2
    )

We use a nested list comprehension to check if any pattern from list2 matches any element from list1 using regular expressions. We replace * with .* in each pattern using the replace() method, and check whether the resulting regular expression matches any element in list1 using another generator expression and the any() function. We then use the all() function to check whether all patterns in list2 match at least one element in list1.

Here's how you can use this function with the two test cases you provided:

# Test the function with the provided examples
list1 = ["aaa_123.csv", "bbb_456.csv", "ccc_789.csv", "aaa_321.csv"]
list2 = ["aaa*.csv", "bbb*.csv", "ccc*.csv"]
result = compare_lists(list1, list2)
print(result)  # should print True

list1 = ["aaa_123.csv", "bbb_456.csv", "aaa_321.csv"]
list2 = ["aaa*.csv", "bbb*.csv", "ccc*.csv", "ddd*.csv"]
result = compare_lists(list1, list2)
print(result)  # should print Fals

Results:

True
False

I hope this helps you achieve what you're looking for!

huangapple
  • 本文由 发表于 2023年7月14日 05:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定