list of unique non-subset sets

Raymond Hettinger vze4rx4y at verizon.net
Thu Mar 17 15:19:01 EST 2005


[les_ander at yahoo.com]
> I have many set objects some of which can contain same group of object
> while others can be subset of the other. Given a list of sets,
> I need to get a list of unique sets such that non of the set is an
> subset of another or contain exactly the same members.
>
> Tried to do the following:
> s1=set(['a','b','c'])
> s2=set(['a','c'])
> s3=set(['a','d','e','f'])
> s4=set(['r','k','l'])
> s5=set(['r','k','l'])
> L=[s1,s2,s3,s4,s5]
> ----------------------- > cleaned-up list should contain s1, s3, s5

This should do the trick:


result = []
for s1 in L:
    for s2 in result:
        if s1 <= s2:
            break
    else:
        result.append(s1)

print result


Raymond Hettinger





More information about the Python-list mailing list