ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 30 May 2020 02:34:25 +0200Please help me write a function that receives a set and then gives all its subsetshttps://ask.sagemath.org/question/51634/please-help-me-write-a-function-that-receives-a-set-and-then-gives-all-its-subsets/Please help me write a function that receives a set and then gives all its subsets Sat, 30 May 2020 01:49:45 +0200https://ask.sagemath.org/question/51634/please-help-me-write-a-function-that-receives-a-set-and-then-gives-all-its-subsets/Answer by tmonteil for <p>Please help me write a function that receives a set and then gives all its subsets </p>
https://ask.sagemath.org/question/51634/please-help-me-write-a-function-that-receives-a-set-and-then-gives-all-its-subsets/?answer=51636#post-id-51636For 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 `Set`s :
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}}Sat, 30 May 2020 02:34:25 +0200https://ask.sagemath.org/question/51634/please-help-me-write-a-function-that-receives-a-set-and-then-gives-all-its-subsets/?answer=51636#post-id-51636