Combinations between elements in two tuples in Python -
this question has answer here:
- all combinations of list of lists 5 answers
- is there math ncr function in python? [duplicate] 2 answers
i have 2 tuples:
t1 = ('a', 'b') t2 = ('c', 'd', 'e')
i wonder how create combinations between tuples, result should be:
ac, ad, ae, bc, bd,
edit
using
list(itertools.combinations('abcd',2))
i generate list of combinations given string:
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
if insert tuple instead of string following error occurs:
typeerror: sequence item 0: expected string, tuple found
any suggestion how proceed?
itertools.product 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) x in itertools.product(t1, t2)] ['ac', 'ad', 'ae', 'bc', 'bd', 'be']
Comments
Post a Comment