好的,感谢Niall Byrne,我想到了这个:
from itertools import productdef knight_moves(position): x, y = position moves = list(product([x-1, x+1],[y-2, y+2])) + list(product([x-2,x+2],[y-1,y+1])) moves = [(x,y) for x,y in moves if x >= 0 and y >= 0 and x < 8 and y < 8] return moves



