1

lst = [2, 2, 2, 3, 5, 7]

I want to partition it into two or more sets. Here is my code

import more_itertools as mit
import pprint as pp
from operator import itemgetter
from itertools import groupby
x = [part for k in range(1, len(lst) + 1) for part in mit.set_partitions(lst, k)]
x.sort()
x = list(map(itemgetter(0), groupby(x)))
pp.pprint(x)

Here is my current output:

[[[2], [2], [2], [3], [5], [7]],
 [[2], [2], [2], [3], [5, 7]],
 [[2], [2], [2], [3, 5], [7]],
 [[2], [2], [2], [3, 5, 7]],
 [[2], [2], [2], [5], [3, 7]],
 [[2], [2], [2, 3], [5], [7]],
 [[2], [2], [2, 3], [5, 7]],
 [[2], [2], [2, 3, 5], [7]],
 [[2], [2], [2, 3, 5, 7]],
 [[2], [2], [2, 5], [3, 7]],
 [[2], [2], [3], [2, 5], [7]],
 [[2], [2], [3], [2, 5, 7]],
 [[2], [2], [3], [5], [2, 7]],
 [[2], [2], [3, 5], [2, 7]],
 [[2], [2], [5], [2, 3, 7]],
 [[2], [2, 2], [3], [5], [7]],
 [[2], [2, 2], [3], [5, 7]],
 [[2], [2, 2], [3, 5], [7]],
 [[2], [2, 2], [3, 5, 7]],
 [[2], [2, 2], [5], [3, 7]],
 [[2], [2, 2, 3], [5], [7]],
 [[2], [2, 2, 3], [5, 7]],
 [[2], [2, 2, 3, 5], [7]],
 [[2], [2, 2, 3, 5, 7]],
 [[2], [2, 2, 5], [3, 7]],
 [[2], [2, 3], [2, 5], [7]],
 [[2], [2, 3], [2, 5, 7]],
 [[2], [2, 3], [5], [2, 7]],
 [[2], [2, 3, 5], [2, 7]],
 [[2], [2, 5], [2, 3, 7]],
 [[2], [3], [2, 2, 5], [7]],
 [[2], [3], [2, 2, 5, 7]],
 [[2], [3], [2, 5], [2, 7]],
 [[2], [3], [5], [2, 2, 7]],
 [[2], [3, 5], [2, 2, 7]],
 [[2], [5], [2, 2, 3, 7]],
 [[2, 2], [2], [3], [5], [7]],
 [[2, 2], [2], [3], [5, 7]],
 [[2, 2], [2], [3, 5], [7]],
 [[2, 2], [2], [3, 5, 7]],
 [[2, 2], [2], [5], [3, 7]],
 [[2, 2], [2, 3], [5], [7]],
 [[2, 2], [2, 3], [5, 7]],
 [[2, 2], [2, 3, 5], [7]],
 [[2, 2], [2, 3, 5, 7]],
 [[2, 2], [2, 5], [3, 7]],
 [[2, 2], [3], [2, 5], [7]],
 [[2, 2], [3], [2, 5, 7]],
 [[2, 2], [3], [5], [2, 7]],
 [[2, 2], [3, 5], [2, 7]],
 [[2, 2], [5], [2, 3, 7]],
 [[2, 2, 2], [3], [5], [7]],
 [[2, 2, 2], [3], [5, 7]],
 [[2, 2, 2], [3, 5], [7]],
 [[2, 2, 2], [3, 5, 7]],
 [[2, 2, 2], [5], [3, 7]],
 [[2, 2, 2, 3], [5], [7]],
 [[2, 2, 2, 3], [5, 7]],
 [[2, 2, 2, 3, 5], [7]],
 [[2, 2, 2, 3, 5, 7]],
 [[2, 2, 2, 5], [3, 7]],
 [[2, 2, 3], [2, 5], [7]],
 [[2, 2, 3], [2, 5, 7]],
 [[2, 2, 3], [5], [2, 7]],
 [[2, 2, 3, 5], [2, 7]],
 [[2, 2, 5], [2, 3, 7]],
 [[2, 3], [2, 2, 5], [7]],
 [[2, 3], [2, 2, 5, 7]],
 [[2, 3], [2, 5], [2, 7]],
 [[2, 3], [5], [2, 2, 7]],
 [[2, 3, 5], [2, 2, 7]],
 [[2, 5], [2, 2, 3, 7]],
 [[3], [2, 2, 2, 5], [7]],
 [[3], [2, 2, 2, 5, 7]],
 [[3], [2, 2, 5], [2, 7]],
 [[3], [2, 5], [2, 2, 7]],
 [[3], [5], [2, 2, 2, 7]],
 [[3, 5], [2, 2, 2, 7]],
 [[5], [2, 2, 2, 3, 7]]]

I have managed to remove some amount of redundancy by sorting followed by using groupby(x) as you can see but there are more redundancies like

[[2], [2, 2], [3], [5], [7]]

and

[[2, 2], [2], [3], [5], [7]]

for me are one and the same thing since order is not important to me.

Please dont close the question without putting in an effort. I have framed the question only after going through other similar questions on stackoverflow, they are for ordered sets, mine is for unordered sets.(part of my code were formed from those answers)

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129

1 Answers1

0

You basically need to construct a set somehow so that you only have unique combinations in your result(removing all permutations of a list except 1). This can be done by sorting each sublist of the list and then constructing a dictionary.

import more_itertools as mit
import pprint as pp
from operator import itemgetter
from itertools import groupby
lst=[2, 2, 2, 3, 5, 7]
x = [part for k in range(1, len(lst) + 1) for part in mit.set_partitions(lst, k)]
x.sort()
x = list(map(itemgetter(0), groupby(x)))

# sort each sublist
for temp in x:
    temp = temp.sort()

# create a dictionary(hashtable), key of a dictionary should be immutable therefore stringified it
unique_combinations = {str(temp):temp for temp in x}
# since unique keys will have unique values, we have unique combinations here
unique_combinations = list(unique_combinations.values())
pp.pprint(unique_combinations)

Output

[[[2], [2], [2], [3], [5], [7]],
 [[2], [2], [2], [3], [5, 7]],
 [[2], [2], [2], [3, 5], [7]],
 [[2], [2], [2], [3, 5, 7]],
 [[2], [2], [2], [3, 7], [5]],
 [[2], [2], [2, 3], [5], [7]],
 [[2], [2], [2, 3], [5, 7]],
 [[2], [2], [2, 3, 5], [7]],
 [[2], [2], [2, 3, 5, 7]],
 [[2], [2], [2, 5], [3, 7]],
 [[2], [2], [2, 5], [3], [7]],
 [[2], [2], [2, 5, 7], [3]],
 [[2], [2], [2, 7], [3], [5]],
 [[2], [2], [2, 7], [3, 5]],
 [[2], [2], [2, 3, 7], [5]],
 [[2], [2, 2], [3], [5], [7]],
 [[2], [2, 2], [3], [5, 7]],
 [[2], [2, 2], [3, 5], [7]],
 [[2], [2, 2], [3, 5, 7]],
 [[2], [2, 2], [3, 7], [5]],
 [[2], [2, 2, 3], [5], [7]],
 [[2], [2, 2, 3], [5, 7]],
 [[2], [2, 2, 3, 5], [7]],
 [[2], [2, 2, 3, 5, 7]],
 [[2], [2, 2, 5], [3, 7]],
 [[2], [2, 3], [2, 5], [7]],
 [[2], [2, 3], [2, 5, 7]],
 [[2], [2, 3], [2, 7], [5]],
 [[2], [2, 3, 5], [2, 7]],
 [[2], [2, 3, 7], [2, 5]],
 [[2], [2, 2, 5], [3], [7]],
 [[2], [2, 2, 5, 7], [3]],
 [[2], [2, 5], [2, 7], [3]],
 [[2], [2, 2, 7], [3], [5]],
 [[2], [2, 2, 7], [3, 5]],
 [[2], [2, 2, 3, 7], [5]],
 [[2, 2], [2, 3], [5], [7]],
 [[2, 2], [2, 3], [5, 7]],
 [[2, 2], [2, 3, 5], [7]],
 [[2, 2], [2, 3, 5, 7]],
 [[2, 2], [2, 5], [3, 7]],
 [[2, 2], [2, 5], [3], [7]],
 [[2, 2], [2, 5, 7], [3]],
 [[2, 2], [2, 7], [3], [5]],
 [[2, 2], [2, 7], [3, 5]],
 [[2, 2], [2, 3, 7], [5]],
 [[2, 2, 2], [3], [5], [7]],
 [[2, 2, 2], [3], [5, 7]],
 [[2, 2, 2], [3, 5], [7]],
 [[2, 2, 2], [3, 5, 7]],
 [[2, 2, 2], [3, 7], [5]],
 [[2, 2, 2, 3], [5], [7]],
 [[2, 2, 2, 3], [5, 7]],
 [[2, 2, 2, 3, 5], [7]],
 [[2, 2, 2, 3, 5, 7]],
 [[2, 2, 2, 5], [3, 7]],
 [[2, 2, 3], [2, 5], [7]],
 [[2, 2, 3], [2, 5, 7]],
 [[2, 2, 3], [2, 7], [5]],
 [[2, 2, 3, 5], [2, 7]],
 [[2, 2, 5], [2, 3, 7]],
 [[2, 2, 5], [2, 3], [7]],
 [[2, 2, 5, 7], [2, 3]],
 [[2, 3], [2, 5], [2, 7]],
 [[2, 2, 7], [2, 3], [5]],
 [[2, 2, 7], [2, 3, 5]],
 [[2, 2, 3, 7], [2, 5]],
 [[2, 2, 2, 5], [3], [7]],
 [[2, 2, 2, 5, 7], [3]],
 [[2, 2, 5], [2, 7], [3]],
 [[2, 2, 7], [2, 5], [3]],
 [[2, 2, 2, 7], [3], [5]],
 [[2, 2, 2, 7], [3, 5]],
 [[2, 2, 2, 3, 7], [5]]]
Rinkesh P
  • 588
  • 4
  • 13
  • Thanks , it gets the job done. But sorting and hashing multiple time is slowing it down . Maybe the time complexity is increasing a lot . So I was wondering if we could restrict it at the point of generation itself. Here is the logic of [`set_partitions`](https://github.com/more-itertools/more-itertools/blob/master/more_itertools/more.py#L3184) and corresponding [ticket](https://github.com/more-itertools/more-itertools/issues/616). Lets discuss there for further improvements. – ishandutta2007 May 24 '22 at 00:11