0

My apologies if this has been asked before, but I'm trying to implement the itertools permutations and combinations tools, but I can't get the packages to give the precise output I'm looking for. For instance, with the input:

[2,3,5,7,11,13,17,19]

I wish to output all possible ways of 'splitting' the list into tuples (not necessarily keeping the order of the given list):

[(2,3)(5)(7)(11)(13)(17)(19), (2,5)(3)(7)(11)(13)(17)(19), ..., (2,3,7)(5,11,17)(13)(19), ..., (2,3,5,7,11)(13,17,19)]

In other words, you can have more than one combination per element in the new list.

However I've got the following code (not claiming authorship to this line, though) to output all the isolated tuples:

[a for l in range(2,5) for a in itertools.combinations([2,3,5,7,11,13,17,19], l)].

i.e.:

[(2, 3), (2, 5), (2, 7), (2, 11), ..., (7, 13, 17, 19), (11, 13, 17, 19)]

Is there a function that can be used to give the desired output? (i.e. all possible groupings within all elements

Thank you!

  • What do you mean by `(5)(7)` etc.? When I run it in python I get "TypeError: 'int' object is not callable". – rpoleski May 20 '20 at 12:36
  • Have a look, if this is not what you actually want: https://stackoverflow.com/questions/19368375/set-partitions-in-python – ctenar May 20 '20 at 13:18
  • I mean that each element that is not in a larger group is in a group of 1. For instance, (2,3)(5)(7)(11)(13)(17)(19) would be a group where 2,3 are placed into one 2-tuple, and the others into 1-tuples. They're not necessary features, so if there's a way without featuring the 1-tuples, that'd work too. Sorry about the code, 'combinations' should've been prefixed by 'itertools'. Changed now :-) – DaMnGoodCoffee May 20 '20 at 13:18
  • ctenar - That's exactly what I'm looking for, thank you! :-) – DaMnGoodCoffee May 20 '20 at 13:22

0 Answers0