擅长:python、mysql、java
<p>一种解决方案是使用枚举:</p>
<pre><code>for idx, char in enumerate(sentence):
# Do what you need to do here, idx is the index of the char,
# char is the char itself
letter = sentence[idx + 2] # would get you the char 2 places ahead in the list
</code></pre>
<p>这样,您可以通过添加到idx来索引。但一定要检查一下列表的末尾。你知道吗</p>
<p>您还可以通过对列表长度使用模数来环绕列表,因此如果在26个元素的列表中的索引25中添加2,则会得到27%26=1,因此再次使用列表的第二个元素。你知道吗</p>