I would like to generate a list of all possible 4-tuples given an array of size n . n is at least 8, so you can always find at least 1 pair.
As an example that helps to understand the problem, I use a smaller version of the problem, pairs of 2-tuples given by an array of size 5 . The expected result for pairs of 2-tuples would lead to 15 points (tuples are ordered, no duplicates):
[(1,2), (3,4)], [(1,2), (3,5)], [(1,2), (4,5)], [(1,3), (2,4)], [(1,3), (2,5)], [(1,3), (4,5)], [(1,4), (2,3)], [(1,4), (2,5)], [(1,4), (3,5)], [(1,5), (2,3)], [(1,5), (2,4)], [(1,5), (3,4)], [(2,3), (4,5)], [(2,4), (3,5)], [(2,5), (3,4)]
My current way to do this is to use itertools from python and go through all the elements returned by itertools.combinations , do 2 itertools.combinations and find 2 pairs that don't share one element, and then work with that element.
To express this in python code, I prepared a small snippet:
arr = list(range(30)) # example list comb = list(itertools.combinations(range(0, len(arr)), 4)) for c1 in comb: for c2 in comb: # go through all possible pairs if len([val for val in c1 if val in c2]) == 0: # intersection of both sets results in 0, so they don't share an element ... # do something and check for duplicates
This method does its job, but is inefficient due to two cycles and works only for small n in a given period of time. Could this be more effective?
Update: after some answers I appreciated the suggestions. The best for my particular case is the (advanced) algorithm provided by the (now deleted) MSeifert response, which performs the fastest:
def generate_four_pairs(n): valids = range(0, n) for x00, x01, x02, x03, x10, x11, x12, x13 in itertools.combinations(valids, 8): yield [x00, x01, x02, x03], [x10, x11, x12, x13] yield [x00, x01, x02, x10], [x03, x11, x12, x13] yield [x00, x01, x02, x11], [x03, x10, x12, x13] yield [x00, x01, x02, x12], [x03, x10, x11, x13] yield [x00, x01, x02, x13], [x03, x10, x11, x12] yield [x00, x01, x03, x10], [x02, x11, x12, x13] yield [x00, x01, x03, x11], [x02, x10, x12, x13] yield [x00, x01, x03, x12], [x02, x10, x11, x13] yield [x00, x01, x03, x13], [x02, x10, x11, x12] yield [x00, x01, x10, x11], [x02, x03, x12, x13] yield [x00, x01, x10, x12], [x02, x03, x11, x13] yield [x00, x01, x10, x13], [x02, x03, x11, x12] yield [x00, x01, x11, x12], [x02, x03, x10, x13] yield [x00, x01, x11, x13], [x02, x03, x10, x12] yield [x00, x01, x12, x13], [x02, x03, x10, x11] yield [x00, x02, x03, x10], [x01, x11, x12, x13] yield [x00, x02, x03, x11], [x01, x10, x12, x13] yield [x00, x02, x03, x12], [x01, x10, x11, x13] yield [x00, x02, x03, x13], [x01, x10, x11, x12] yield [x00, x02, x10, x11], [x01, x03, x12, x13] yield [x00, x02, x10, x12], [x01, x03, x11, x13] yield [x00, x02, x10, x13], [x01, x03, x11, x12] yield [x00, x02, x11, x12], [x01, x03, x10, x13] yield [x00, x02, x11, x13], [x01, x03, x10, x12] yield [x00, x02, x12, x13], [x01, x03, x10, x11] yield [x00, x03, x10, x11], [x01, x02, x12, x13] yield [x00, x03, x10, x12], [x01, x02, x11, x13] yield [x00, x03, x10, x13], [x01, x02, x11, x12] yield [x00, x03, x11, x12], [x01, x02, x10, x13] yield [x00, x03, x11, x13], [x01, x02, x10, x12] yield [x00, x03, x12, x13], [x01, x02, x10, x11] yield [x00, x10, x11, x12], [x01, x02, x03, x13] yield [x00, x10, x11, x13], [x01, x02, x03, x12] yield [x00, x10, x12, x13], [x01, x02, x03, x11] yield [x00, x11, x12, x13], [x01, x02, x03, x10] yield [x01, x02, x03, x00], [x10, x11, x12, x13] yield [x01, x02, x03, x10], [x00, x11, x12, x13] yield [x01, x02, x03, x11], [x00, x10, x12, x13] yield [x01, x02, x03, x12], [x00, x10, x11, x13] yield [x01, x02, x03, x13], [x00, x10, x11, x12] yield [x01, x02, x10, x00], [x03, x11, x12, x13] yield [x01, x02, x10, x11], [x00, x03, x12, x13] yield [x01, x02, x10, x12], [x00, x03, x11, x13] yield [x01, x02, x10, x13], [x00, x03, x11, x12] yield [x01, x02, x11, x00], [x03, x10, x12, x13] yield [x01, x02, x11, x12], [x00, x03, x10, x13] yield [x01, x02, x11, x13], [x00, x03, x10, x12] yield [x01, x02, x12, x00], [x03, x10, x11, x13] yield [x01, x02, x12, x13], [x00, x03, x10, x11] yield [x01, x02, x13, x00], [x03, x10, x11, x12] yield [x01, x03, x10, x00], [x02, x11, x12, x13] yield [x01, x03, x10, x11], [x00, x02, x12, x13] yield [x01, x03, x10, x12], [x00, x02, x11, x13] yield [x01, x03, x10, x13], [x00, x02, x11, x12] yield [x01, x03, x11, x00], [x02, x10, x12, x13] yield [x01, x03, x11, x12], [x00, x02, x10, x13] yield [x01, x03, x11, x13], [x00, x02, x10, x12] yield [x01, x03, x12, x00], [x02, x10, x11, x13] yield [x01, x03, x12, x13], [x00, x02, x10, x11] yield [x01, x03, x13, x00], [x02, x10, x11, x12] yield [x01, x10, x11, x00], [x02, x03, x12, x13] yield [x01, x10, x11, x12], [x00, x02, x03, x13] yield [x01, x10, x11, x13], [x00, x02, x03, x12] yield [x01, x10, x12, x00], [x02, x03, x11, x13] yield [x01, x10, x12, x13], [x00, x02, x03, x11] yield [x01, x10, x13, x00], [x02, x03, x11, x12] yield [x01, x11, x12, x00], [x02, x03, x10, x13] yield [x01, x11, x12, x13], [x00, x02, x03, x10] yield [x01, x11, x13, x00], [x02, x03, x10, x12] yield [x01, x12, x13, x00], [x02, x03, x10, x11]
For a general approach, I would suggest the answer provided by NPE, as this is the shortest and easiest readable answer for this problem.