0

Given a set of numbers, say, {A,B,C,D,E}, I wish to generate the set partitions like:

{A}, {B}, {C}, {D}, {E}
{A,B}, {C}, {D}, {E}
{A,B}, {C,D}, {E}
{A,B,E}, {C,D}
{A,B}, {C,D,E} # etc.  

Is there some Pythonic way to do this?

Edit1: Note that I have removed one of the original set that I had included - {{A,B,C,D,E}}. I want to generates sets of a maximum size of 3. Then how do I achieve this?

Edit2: This is not a duplicate because the marked question is pretty different. I have an upper limit of a size of 3.

  • Take a look at `itertools`, it might have something like this. – Barmar Sep 29 '17 at 22:56
  • If not, you'll probably need to code it yourself, using a recursive algorithm. – Barmar Sep 29 '17 at 22:57
  • 1
    https://stackoverflow.com/questions/19368375/set-partitions-in-python this is also the same question. – ayhan Sep 29 '17 at 23:05
  • Yep, that's a better one. Although it's recursive on the accepted answer, dynamic programming is much better for this. If this question is still open in an hour or so, I'll post the DP algo. – wim Sep 29 '17 at 23:08

0 Answers0