Ask Your Question

Revision history [back]

For Sage Set, there is a method that does that out of the box:

sage: s = Set([1,2,3]) ; s
{1, 2, 3}
sage: s.subsets()
Subsets of {1, 2, 3}
sage: list(s.subsets())
[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]

For Pyyhon set you can use some itertools or go back and forth to Sage Sets :

sage: def powerset(s):
....:     return set(Set(s).subsets())
sage: S = {1,2,3}
sage: powerset(S)
{{1, 3}, {}, {1}, {3}, {1, 2, 3}, {1, 2}, {2, 3}, {2}}

For Sage Set, (see the uppercase), there is a method that does that out of the box:

sage: s = Set([1,2,3]) ; s
{1, 2, 3}
sage: s.subsets()
Subsets of {1, 2, 3}
sage: list(s.subsets())
[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]

For Pyyhon set (see the lowercase) you can use some itertools or go back and forth to Sage Sets :

sage: def powerset(s):
....:     return set(Set(s).subsets())
sage: S = {1,2,3}
sage: powerset(S)
{{1, 3}, {}, {1}, {3}, {1, 2, 3}, {1, 2}, {2, 3}, {2}}