I wonder if someone can help with the following task: What is the way to get all combinations a list can be split into sublists, when order does not matter?
Let's say I have a list of 4 items:
import itertools as it
a = [1, 2, 3, 4]
print(list(it.combinations(a, 2)))
That will give me a list of 6 possible pairs:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
How to make (out of it? or any other way) a set of lists that contain original [1, 2, 3, 4] sequence in any order? So for this example it will contain three sublists:
[(1, 2), (3, 4)]
[(1, 3), (2, 4)]
[(1, 4), (2, 3)]
UPDATE:
A small clarification:
In other words, I need to get all sets of n-tuples such that their members contain all the population of original list, when the order within an n-tuple does not matter. Thus [(1, 2), (3, 4)] is ok, but [(2, 1), (3, 4)] is not needed because it is the same as the first set, if we ignore the order.
UPDATE2:
So for the list of length 6, and for chunks of size 2 this fun function should work as follows:
import itertools as it
a = [1, 2, 3, 4, 5, 6,]
r = 2
# fun(a,r):
# OUT:
# [
# (1, 2), (3, 4), (5, 6)
# (1, 3), (2, 4), (5, 6),
# (1, 4), (2, 3), (5, 6),
# (1, 5), (2, 3), (4, 6),
# (1, 6), (2, 3), (4, 5),
# ]
print(list(it.permutations(a, 4)))? Might that be what you're looking for?[1, 2, 3, 4]sequence in any order?" For that you would simply reshuffle using something likerandom.shuffle. What your output is telling me is that you want to randomly pick pairs fromlist(it.combinations(a, 2)). Could you clarify a bit?[(1, 2), (3, 4)]is ok, but[(2, 1), (3, 4)]is not needed because it is the same as the first set, if we ignore the order