-3

I have two tuples:

t1 = ('A', 'B')
t2 = ('C', 'D', 'E')

I wonder how to create combinations between tuples, so the result should be:

AC, AD, AE, BC, BD, BE

EDIT

Using

list(itertools.combinations('abcd',2))

I could generate list of combinations for a given string:

[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]

If I insert tuple instead of string the following error occurs:

TypeError: sequence item 0: expected string, tuple found

Any suggestion how to proceed?

Andrej
  • 3,719
  • 11
  • 44
  • 73
  • 3
    You will greatly increase your chances of getting an answer for your question if you include your input, _what you have tried_, your expected output vs. your actual output and the full stack trace of any errors you receive. You can also read [this guide](http://stackoverflow.com/help/mcve) – kylieCatt Jun 29 '15 at 15:57
  • 2
    itertools has something for it – ThiefMaster Jun 29 '15 at 15:57
  • What does nCr have to do with that? – Dakkaron Jun 29 '15 at 15:59
  • 1
    ^nCr would list all the possible combinations of values from 2 sets, so that would be a perfect fit. – Danny Dircz Jun 29 '15 at 16:03
  • 1
    Ah, ok, I see, that question wasn't actually about the math nCr function that would just calculate the amount of calculations. Makes sense, and yes, that is a possible duplicate. – Dakkaron Jun 29 '15 at 16:04
  • The apparent goal here is to determine the pairings, not count them. Furthermore, these are **not "combinations"**. Thus, the prior duplicate of the nCr question was in fact completely inappropriate. – Karl Knechtel Feb 18 '23 at 16:46

6 Answers6

17

itertools.product does exactly what you are looking for:

>>> import itertools
>>> t1 = ('A', 'B')
>>> t2 = ('C', 'D', 'E')
>>> list(itertools.product(t1, t2))
[('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E')]
>>> [''.join(x) for x in itertools.product(t1, t2)]
['AC', 'AD', 'AE', 'BC', 'BD', 'BE']
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2
for value_one in t1:
    for value_two in t2:
        result = (str(value_one), str(value_two))
        print result

This uses no external libraries. Literally just two for loops and string concatenation. Format the output however you'd like.

This seems like you either did not put any effort into finding this answer, or I am misinterpreting the question.

Edit: I see that you are coming from an R background, so you may not understand Python syntax. Please refer to guides for Python basics; I believe they will help greatly. To save the values from the loop, as @Naman mentioned, you will want to make an empty list and [list_name].append([value]) the desired values, then print the values in the list using other constructs.

Matthew R.
  • 438
  • 4
  • 11
2

All possible combinations:

import itertools

t1 = ('A', 'B')
t2 = ('C', 'D', 'E')

print(tuple(itertools.combinations(t1 + t2, 2)))

Output: (('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'D'), ('C', 'E'), ('D', 'E'))

Red Shift
  • 1,312
  • 2
  • 17
  • 29
0

Here is my simple method:

for i in t1:
    for j in t2:
        print(i+j,end="")

This three line of input gives the above combinations.

user2738777
  • 418
  • 1
  • 5
  • 21
  • Best not to rely on a bit of print syntax to join them together on one line, when the OP might not want to print it. `string.join` is better. – Rob Grant Jun 30 '15 at 08:19
0

You can print what you want by iterating over both the tuples(or make an empty list and store your output in that list)

l = []
for c1 in t1:
    for c2 in t2:
        print c1 + c2 + ',',
        l.append(c1 + c2)

Finally list l will contain the output elements. You can process its elements or make a tuple of it by

t = tuple(l)
Naman Sogani
  • 943
  • 1
  • 8
  • 28
0
t = []
for x in t1:
    for y in t2:
        t.append(x+y)

t = tuple(t)

So iterate over both tuples, append every combination to a list and then convert the list back to a tuple.

Dakkaron
  • 5,930
  • 2
  • 36
  • 51