Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在一个列表中有几个列表,它们形成了一个三分球游戏:
list = [ ['X', 'O', 'O'], ['O', 'X', 'O'], [' ', 'X', ' '] ]
我需要编写一个函数,从左上角到右下角,然后从右上角到左下角返回游戏的对角线,因此输出将是:
diags = (['X', 'X', ' '],['O', 'X', ' '])
我已经尝试过嵌套 for 循环的各种组合,但似乎无法理解它。
nw_to_se = [your_list[i][i] for i in range(3)] ne_to_sw = [your_list[i][2-i] for i in range(3)] diags = (nw_to_se, ne_to_sw)
代替[2-i]你也可以使用[-i-1],它可以缩放到任何大小的正方形。
[2-i]
[-i-1]