<p>我会这样分解问题:</p>
<ol>
<li><p>首先,创建一个函数,该函数可以替换给定的替换字母集。我的输入规范是一个字母序列,其中第一个字母是<code>'0'</code>字符的替换,第二个字母是<code>1</code>等字符的替换。这允许我使用该序列中的索引来确定要替换的字符,同时生成一个普通序列,而不是dict或其他复杂结构。要进行替换,我将使用原始字符串的内置<code>translate</code>方法。这需要一个描述为<a href="https://docs.python.org/3.8/library/stdtypes.html#str.translate" rel="nofollow noreferrer">in the documentation</a>的字典,我可以用dict理解或提供的helper方法<code>str.maketrans</code>(一种<code>str</code>类型的静态方法)轻松构建它</p>
</li>
<li><p><a href="https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists">Use ^{<cd6>}</a>生成这些序列</p>
</li>
<li><p>使用列表理解为每个序列应用替换</p>
</li>
</ol>
<p>因此:</p>
<pre><code>from itertools import product
def replace_digits(original, replacement):
# translation = {ord(str(i)): c for i, c in enumerate(replacement)}
translation = str.maketrans('0123456789', ''.join(replacement))
print(translation)
return original.translate(translation)
replacements = product(
['o', 'q'], ['l', 'i'], ['r', 'z'], ['e', 'b'], ['a'],
['s'], ['b', 'g'] , ['t'], ['b'], ['g', 'p']
)
[replace_digits('14732', r) for r in replacements]
</code></pre>
<p>(您会注意到结果中存在重复;这是因为输入中没有出现符号的变体替换。)</p>