为什么说列表索引超出范围?这可能不是最好的方法,但只是想知道为什么会这样!谢谢
key = 13
alphabet = ["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"]
replacement_set = ["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"]
count = -1
while count <= 25:
count += 1
if count < int(key):
index_value = int(count + int(key))
replacement_set[index_value] = alphabet[count]
print replacement_set
elif count >= int(key):
index_value = int(count - int(key))
replacement_set[index_value] = alphabet[count]
print replacement_set
else:
break
print replacement_set
请您参考如下方法:
Simeon Visser 的回答回答了你的问题,但我认为仍然值得指出你的问题的 Pythonic 解决方案:
from string import ascii_uppercase as uppercase
replacement_set = uppercase[13:] + uppercase[:13]
uppercase[:13] 是切片符号的一个例子。您可以从 the Python documentation 了解更多信息。 .
