'words amalgamation

I am new user learning python. I have query if it can be done or not. If user input a word suppose "Dance" and characters are "$" and "@", he would get a all possibilities of word and character combination for example ['D$@a$@n@$c@$e',D@$a@$n@$c@$e, 'Da$n$c$e', 'D$@an$c@$e', 'Da@nc$e'], etc. It should give a combinations. Is it possible to achieve?

import itertools as it

string = 'ABCD'  # input('enter string: ')
fill_char = '$'  # input('enter fill_char: ')

fillers = it.product(['', fill_char], repeat=len(string))

for filler in fillers:
    tmp = it.zip_longest(string, filler)
    print(''.join([''.join(char_and_fill_char) for char_and_fill_char in tmp]))


Output:
ABCD
ABCD$
ABC$D
ABC$D$
AB$CD
AB$CD$
AB$C$D
AB$C$D$
A$BCD
A$BCD$
A$BC$D
A$BC$D$
A$B$CD
A$B$CD$
A$B$C$D
A$B$C$D$

I wanted combination of 3 characters. If anybody could help I would be very greatful.



Solution 1:[1]

Here is one possible solution: For more clear presentation I only chose a string of two letters. Basically take every single letter of your string and join all possible combinations of fill_chars to it. Then build the product of all lists (each containing all combinations for one letter). Added several comments and print statements for explanation.

from itertools import permutations, product
string = 'AB'  # input('enter string: ')
fill_char = '$@'  # input('enter fill_char: ')

s_lst = list(string)
fill_lst = list(fill_char)
powerSet = []

for char in s_lst:
    perm_each_char=[]
    
    for k in range(len(fill_char)+1):
        tmp = [''.join(p) for p in permutations(fill_lst, k)]
        # tmp : permutation result in list, each joined together to string (e.g k=2: ['@$', '$@'])
        tmp2 = [''.join((char,elem)) for elem in tmp]
        # tmp2 : list of current char joined with each item in list of tmp (e.g. k=2: ['A@$', 'A$@'])
        perm_each_char.extend(tmp2)
        print(f"{char=} - {k=} - {perm_each_char=}")
        
    powerSet.append(perm_each_char) # list of lists, each list contains all permutations of one char of the string
    print(f"combined: {powerSet=} \n")
    
all_combinations = list(''.join(x) for x in product(*powerSet))
# now you build the itertools.product with all lists to get every combination
print(f"{all_combinations=}")

Output:

char='A' - k=0 - perm_each_char=['A']
char='A' - k=1 - perm_each_char=['A', 'A$', 'A@']
char='A' - k=2 - perm_each_char=['A', 'A$', 'A@', 'A$@', 'A@$']
combined: powerSet=[['A', 'A$', 'A@', 'A$@', 'A@$']] 

char='B' - k=0 - perm_each_char=['B']
char='B' - k=1 - perm_each_char=['B', 'B$', 'B@']
char='B' - k=2 - perm_each_char=['B', 'B$', 'B@', 'B$@', 'B@$']
combined: powerSet=[['A', 'A$', 'A@', 'A$@', 'A@$'], ['B', 'B$', 'B@', 'B$@', 'B@$']] 

all_combinations=['AB', 'AB$', 'AB@', 'AB$@', 'AB@$', 'A$B', 'A$B$', 'A$B@', 'A$B$@', 'A$B@$', 'A@B', 'A@B$', 'A@B@', 'A@B$@', 'A@B@$', 'A$@B', 'A$@B$', 'A$@B@', 'A$@B$@', 'A$@B@$', 'A@$B', 'A@$B$', 'A@$B@', 'A@$B$@', 'A@$B@$']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DharmanBot