-1

I have two sets:

a = {x,x,x,x}
b = {x,x,x,x,x}

with x meaning unknown values

The exact arrangements or length or values of each set (a or b) are not fixed until they've been discovered. I am not able to know what exact values or which would be a subset of which but I need to be able to detect and perform some action once either is a subset of the other.

I understand I could do this using:

if a <= b or b <= a:

But the issue is that below this if statement, I must be sure of which one is a subset of the other because I perform some functions with the subset and the set such as filtering out the subset from the set. There's no room for maybes, like an if statement. And I do not want to write duplicate code.

How do you suggest I go about this?

Sources I have checked: Finding all the subsets of a set Finding subset of a set

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Alexander
  • 457
  • 1
  • 4
  • 15

1 Answers1

0

Thanks to @slothrop. This works! No matter the values of any of the list or set.

smaller_set = list(min(set(a), set(b), key=len))
larger_set = list(max(set(a), set(b), key=len))
print(smaller_set,'smaller_set')
print(larger_set,'larger_set')
Alexander
  • 457
  • 1
  • 4
  • 15
  • 1
    To avoid calling `min` and `max` which feels redundant as there are only two values, you can also go with something like `smaller, larger = (a, b) if len(set(a)) < len(set(b)) else (b, a)` (credit https://stackoverflow.com/a/3674252/6045800) – Tomerikoo Aug 15 '23 at 12:51