<p>对我们来说关键的函数是<a href="https://docs.python.org/2/library/functions.html#ord" rel="nofollow noreferrer">^{<cd1>}</a>。此函数接受<code>string</code>并返回其<code>ASCII</code>值的<code>integer</code>。从<a href="http://www.asciitable.com/" rel="nofollow noreferrer">^{<cd4>} table</a>可以看出<code>A</code>位于<code>65</code>。在<code>65</code>之后,下一个<code>26</code>是字母表中的字母。。。你知道吗</p>
<p>知道了这一点,我们可以开始写一些<code>code</code>来找到字符之间的<code>'distances'</code>。首先,我们定义一个<code>string</code>到<code>test</code>的with。接下来,我们将<code>loop</code>通过<code>string</code>中的<code>indexes</code>,直到<code>string</code><code>-1</code>的<code>length</code>,因为我们将计算这个<code>index</code>和之后的<code>index</code>之间的距离。你知道吗</p>
<p>然后我们想计算出这个<code>ASCII</code>处字符的<code>index</code>值和后面<code>index</code>处字符的<code>ASCII</code>值之间的差异。你知道吗</p>
<p>这将是<code>'distance'</code>(一旦我们用<code>abs()</code>取绝对值),但是由于我们使用的是<code>2</code>行,如果差值大于<code>12</code>(即在不同的行上),我们需要从<code>difference</code>中减去<code>12</code>。你知道吗</p>
<p>这听起来可能很复杂,但不需要创建任何我们必须查找的<code>dictionaries</code>或<code>lists</code>。。。你知道吗</p>
<p>我刚才描述的<code>code</code>看起来像:</p>
<pre><code>word = "MAMA"
distance = 0
for i in range(len(word) - 1):
diff = abs(ord(word[i+1]) - ord(word[i]))
if diff > 12:
diff = diff - 12
distance += diff
print(distance)
</code></pre>
<p>它将<code>36</code>作为<code>distance</code>。你知道吗</p>
<p>希望这有帮助!<strong>:)</strong></p>