-4

I have such input masked word like --- and I want to replace all - with numbers(0-9)

Code:

def masker(input_mask,pattern):
 s = list()
 s = (itertools.product(pattern,repeat=1))

 for i in input_mask:
  if ( i=='-'): 
   for j in s :
    print (input_mask.replace('-',''.join(j)))


masker ('-a-' , '123')

but my output is:

1a1
2a2
3a3
1a1
2a2
3a3

And my main goal is this output:

1a1
1a2
1a3
2a1
....
....
3a3
keyvan vafaee
  • 464
  • 4
  • 15

1 Answers1

1

in your code j = '1' in the first run. you then just replace all - with that...


my suggestion would be to use str.format as mask (i.e. replacing your - with {}). then you could do this:

from itertools import product

def masker(input_mask, pattern):
    mask = input_mask.replace('-', '{}')  # mask = '{}a{}'
    for values in product(pattern, repeat=2):
        print(mask.format(*values))

masker(input_mask='-a-', pattern='123')

also note the repeat=2 in the product. to make it more generic you need to use repeat=input_mask.count('-').

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111