0

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?

resonance
  • 1
  • 4
  • 1
    "I would expect the second function to yield frozenset({'E830'})" why would ever yield that? There is no `"E830"` in either the `LCC` or any of the frozen sets, so how could it *possibly* be in the intersection? – juanpa.arrivillaga Nov 22 '22 at 00:01
  • 1
    What do you mean by "with wildcards"? – kaya3 Nov 22 '22 at 00:01
  • There are no "wild cards". Why do you think there should be? Where else would there be wild cards like this? – juanpa.arrivillaga Nov 22 '22 at 00:02
  • 1
    It sounds like what you really want is a regex like `^(A|E...)$`, not a set intersection. – kaya3 Nov 22 '22 at 00:12
  • from what I've seen, the wildcard string match works with regex and fnmatch. However, I don't think frozenset (I need to use frozenset) will accept these, but I could definitely be wrong – resonance Nov 22 '22 at 00:16
  • You are correct. Set intersection in Python looks for an exact match. To use wildcards, you will need to go with regular expressions. And I'm not sure you can see the deleted answer below, but `frozenset('E830')` produces a set with four elements: `'0','3','8', and 'E'`. – Tim Roberts Nov 22 '22 at 00:19
  • Python will not automatically treat strings as regexes just because they have `.` characters in them. If it did, there would be hundreds of questions on Stack Overflow asking why `'.' == 'x'` is true. – kaya3 Nov 22 '22 at 00:24
  • It's unclear. What is your 'wildcards'? Is that '...'? Does it mean any characters of length three? And you seem to understand now that your code is not expected to do a wildcard pattern matching. Then, why not edit your question? – relent95 Nov 22 '22 at 01:45

1 Answers1

0

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'}
relent95
  • 3,703
  • 1
  • 14
  • 17