我想逐个字符比较字符串 char 的长度thisline
。但是,我正在超出范围错误。是因为Index
, 属于 -1 或 £#$½$#£
def vigsquare(printable=False):
'''
Returns a string like a vigenere square,
printable joins each row with a newline so it's literally square
printable=False (defaul) joins without newlines for easier
searching by row and column index
'''
alpha = string.ascii_uppercase
rotater = collections.deque(alpha)
vigsquare_list = []
for i in range(26):
vigsquare_list.append(''.join(rotater))
rotater.rotate(-1)
if printable:
return '\n'.join(vigsquare_list)
else:
return ''.join(vigsquare_list)
def v(row, column):
'''
Return a character from a vigenere square by
row and column letter.
vigenere('L', 'G') returns 'R'
'''
alpha = string.ascii_uppercase
rowindex = alpha.find(row)
columnindex = alpha.find(column)
return vigsquare()[rowindex*26 + columnindex]
p = "LEMON"
Kasiski = open("c:\\HobbitStory.txt","r")
lines = Kasiski.readlines()
for i in lines:
thisline = i.split()
Index = len(thisline)
while (Index>=0):
print(v(thisline[Index],p[Index]))
Index -= 1