Frozenset Intersection with Wildcards

106 Views Asked by At

I'm trying to intersect frozensets in Python, but not getting the desired result. My intersection array, LCC, has 100s of strings.

LCC = ['A','E...']
fs1 = frozenset('A')
fs2 = frozenset('E830')
fs1.intersection(LCC)
fs2.intersection(LCC)

The results are:

frozenset({'A'})
frozenset()

I would expect the second function to yield frozenset({'E830'})

Does anyone known how to get this to work with wildcards? Or is it impossible since the string being passed in to LCC is interpreting the wildcards literally?

1

There are 1 best solutions below

0
On

I assume your '...' is a wildcard pattern meaning any characters of length three.(Regular expression syntax)

You can use regular expressions like this.

import re

LCC = ['A', 'E...']
fs = frozenset({'A', 'E830', 'E2'})

re_patterns = [re.compile(pattern) for pattern in LCC]
intersection = {e for e in fs for pattern in re_patterns
    if re.fullmatch(pattern, e)}
print(intersection)

This will output the following.

{'A', 'E830'}