Unique combinations of a list of tuples

p4rch :

Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)] how would you compute all possible combinations and combinations of subsets?

In this case the result should look like this:

[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9), 
(5), (5,7), (5,8), (5,9), 
(6), (6,7), (6,8), (6,9), 
(7), (8), (9)
]
  • all tuples with identical elements are regarded the same
  • combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution: (1,2), (4,6) or (7,8,9))
Ajax1234 :

You can use recursion with a generator:

data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
   if len(c) == len(d):
     yield c
   else:
     for i in d:
        if i not in c:
           yield from combos(d, c+[i])

def product(d, c = []):
  if c:
    yield tuple(c)
  if d:
    for i in d[0]:
      yield from product(d[1:], c+[i])

result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]

Output:

[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

retain unique combinations from a list of tuples in python

Remove tuples that are duplicate combinations of lists in the list of these tuples

Combinations of list of tuples with alternative values

All possible combinations of a list of tuples

How to get unique combinations of pairs in nested tuples?

Calculating incidence of unique tuples in a list of tuples

Python - Tuples - Retrieve unique elements in list of tuples

Unique List of Tuples By First Value of Tuples

Count unique combinations in List R

Get all possible combinations from a list as tuples

How to construct a list of tuples of all possible combinations

Find all combinations of tuples inside of a list

Getting unique tuples from a list

Count unique tuples in nested list

How to count unique tuples in the list?

Finding unique list in list of list of tuples

How do I transform unique row combinations into sorted tuples

Generate a list of unique combinations from a list

Get all unique combinations from list of permutations

Get unique combinations of elements from a python list

Combine nested list into unique combinations of sublists

Unique combinations from list with "distance limit"

Fastest way to find unique combinations of list

finding unique combinations of elements in a list of lists

Unique combinations of 0 and 1 in list in prolog

Haskell add unique combinations of list to tuple

Grab unique tuples in python list, irrespective of order

Find single unique value in list of tuples

Problem in getting unique elements from list of tuples