#暂时用了2个字典,还没想到更优雅的解决办法,不过代码是没问题的
from itertools import product
def KnightMove(row, col):
convert = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8}
convert1 = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h'}
y, x = row, convert[col]
outlist, border = [], range(1,9)
temlist = list(product([x+1,x-1],[y+2,y-2])) + list(product([x+2,x-2],[y+1,y-1]))
for e in temlist:
if e[0] in border and e[1] in border:
outlist.append( (convert1[e[0]], e[1]) )
return outlist