<p>是的,您应该使用列表理解:</p>
<pre><code>letters = 'abcdefghijklmnopqrstuvwxyz'
ciphertext = input().lower()
indices = [letters.find(c) for c in ciphertext]
</code></pre>
<p>这会将<code>letters.find(c)</code>附加到<code>ciphertext</code>中每个字母(这里称为<code>c</code>)的索引中。你知道吗</p>
<p>但是如果<code>letters</code>始终是字母表(根据维基百科的说法似乎是这样的),你可以通过使用<code>ord</code>来加快速度,它会给你每个字母的ASCII索引。要使<code>'a'</code>成为<code>0</code>,只需减去<code>ord('a')</code>:</p>
<pre><code>ciphertext = input().lower()
indices = [ord(c) - ord('a') for c in ciphertext]
</code></pre>
<p>看看维基百科上的希尔密码,你所描述的似乎并不是你所需要的。你知道吗</p>
<p>我的理解是你的钥匙必须是一个完美的正方形长度(n^2)。然后将键塑造成<code>n</code>乘<code>n</code>矩阵,并将其乘以<code>n</code>纯文本块。你应该调查一下<a href="http://www.numpy.org/" rel="nofollow noreferrer">numpy</a>。它有很多处理矩阵的工具,我很确定你可以用几行字来表达这个密码:</p>
<pre><code>import numpy as np
ciphertext = np.array(map(ord, input().lowercase())) - ord('a')
n = int(sqrt(len(ciphertext)))
assert n**2 == len(ciphertext), 'ciphertext must be perfect square in length'
ciphertext.reshape((n, n)) # Make ciphertext a nxn matrix
# Matrix multiply plaintext chunks of n by ciphertext
plaintext.reshape((n, -1)) @ ciphertext
</code></pre>
<p>似乎密文必须是可逆的。numpy<a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.inv.html" rel="nofollow noreferrer">allows you to check that the ciphertext is invertible (and compute its inverse)</a>无需自己编写linalg代码。你知道吗</p>