May-22-2019, 01:43 AM
from itertools import product import re def string_combos(s): # get all expressions inside brackets into a list brackets = re.findall(r'{(.+?)}', s) # remove comas joined_chars_list = [''.join(b.split(',')) for b in brackets] # In the above line the b.split(',') could be turned into another # list comprehension to remove whitespace if it is acceptable as # a part of input string # E.g. if this is acceptable: # string_combos('{a, b}{ c , d}{ e, f}') # Then use something like: # [char_part.strip() for char_part in b.split(',')] return [''.join(p) for p in product(*joined_chars_list)] # copy of what scidam posted but I find this way easier to read than "list(map(" way result = string_combos('{a,b}{c,d}{e,f}') print(result)
Output:['ace', 'acf', 'ade', 'adf', 'bce', 'bcf', 'bde', 'bdf']