| 1 | initial version |
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}}
| 2 | No.2 Revision |
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}}
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.