使用掩码生成所有可能的数字

use*_*564 3 python combinations mask

假设我有一个字符串:

a = "123**7*9"
Run Code Online (Sandbox Code Playgroud)

我需要生成所有可能的组合:

12300709...12399799
Run Code Online (Sandbox Code Playgroud)

如何用Python做到这一点?

Ash*_*ary 11

您可以使用itertools.product和字符串格式:

>>> from itertools import product
>>> strs = "123**7*9"
>>> c = strs.count("*")              #count the number of "*"'s
>>> strs = strs.replace("*","{}")    #replace '*'s with '{}' for formatting
>>> for x in product("0123456789",repeat=c):
...     print strs.format(*x)               #use `int()` to get an integer

12300709
12300719
12300729
12300739
12300749
12300759
12300769
12300779
12300789
12300799
....
Run Code Online (Sandbox Code Playgroud)