I am curious on how to replace wildcards with multiple elements from a list. For instance:
template = 'w**er'
some_list = ['a', 't']
# I want to create a loop that gives out the output: ['water', 'wtaer']
I tried playing around with my program and it is easy to replace the wildcard ('*') with only one character, however when there are multiple elements in a list, it complicates things and I am not sure how to do it.
Edit:
The loop I made:
for x in range(len(template)):
if template[x] == '*':
for y in range(len(letters)):
template[x] = letters[y]
word = ''.join(template)
store.append(word)
store = ['wa*er', 'wt*er', 'wtaer', 'wtter']
Hope someone can help me!
CodePudding user response:
Perhaps you're looking for itertools.permutations
? You could replace * with a placeholder and use str.format
to fill in:
from itertools import permutations
template = 'w{}{}er'
some_list = ['a', 't']
out = [template.format(*tpl) for tpl in permutations(some_list)]
Output:
['water', 'wtaer']