Skip to content Skip to sidebar Skip to footer

Grouping Integers By Set Membership In Python

Working in Python, given a list of N sets of integers from the range range(s,n), how can I build a list that groups all these integers according to their set memberships? An exampl

Solution 1:

I cringe even thinking about the efficiency of this, but it's pretty compact:

 out = [set(range(x, y))]
 for in_set in input:
    out_diff = [out_set - in_set for out_set in out]
    out_union = [out_set & in_set for out_set in out]
    out = out_diff + out_union

Post a Comment for "Grouping Integers By Set Membership In Python"