<p>你可以记下每个星号的索引</p>
<pre class="lang-py prettyprint-override"><code>asterisk_location = [i for i, x in enumerate(source) if x == "*"]
</code></pre>
<p>您可以利用<a href="https://docs.python.org/3/library/itertools.html#itertools.product" rel="nofollow noreferrer">itertools.product</a>来处理根据出现的星号数生成所有可能的重复对</p>
<pre class="lang-py prettyprint-override"><code>product(astric, repeat=len(asterisk_location))
</code></pre>
<p>然后,您可以将已知星号索引中的每个值替换为itertools生成的值之一</p>
<pre class="lang-py prettyprint-override"><code>source[index] = value
</code></pre>
<p>总之,你最终会得到这样的结果:</p>
<pre class="lang-py prettyprint-override"><code>from itertools import product
from typing import Generator
astric = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
def generate_possible_options(source: str) -> Generator[str, None, None]:
source = list(source)
asterisk_location = [i for i, x in enumerate(source) if x == "*"]
for option in product(astric, repeat=len(asterisk_location)):
for index, value in zip(asterisk_location, option):
source[index] = value
yield ''.join(source)
if __name__ == '__main__':
for possibility in generate_possible_options("m*ep*"):
print(possibility)
</code></pre>
<p>当然,您可以添加用户输入并将结果传递给<code>generate_possible_options</code>函数,但我将其保留为静态值,以便可以轻松复制和运行,而无需挂起用户输入</p>
<p>这产生了1296种可能的选择</p>
<p>缩略输出:</p>
<pre><code>maepa
maepb
maepc
maepd
maepe
maepf
maepg
maeph
maepi
maepj
maepk
maepl
...
m9ep2
m9ep3
m9ep4
m9ep5
m9ep6
m9ep7
m9ep8
m9ep9
</code></pre>
<p>还可以通过以下方式获取列表对象:</p>
<pre class="lang-py prettyprint-override"><code>possibilities = list(generate_possible_options("m*ep*"))
</code></pre>