Search code examples
pythonminesweeper

How to find subset of a set without knowing the subset or parent set values


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


Solution

  • 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')