Python: Rotate nested lists -90°

大憨熊 提交于 2021-01-29 02:55:01

问题


I'm new to programming and I'm having trouble with a school assignment. I need to print the heart from 'Automate the Boring Stuff Chapter 4', (I already did that.) So now I need to rotate it -90°, I tried multiple solutions, (like reversing the range) but nothing works. What im trying to print is this:

....O....
...OOO...
..OOOOO..
.OOOOOOO.
.OOOOOOO.
..OO.OO..

Here is my code (Spoilers ahead):

grid = [['.', '.', '.', '.', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['.', 'O', 'O', 'O', 'O', 'O'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['.', '.', '.', '.', '.', '.']]

for i in range(6):
    for a in range(9):
        if a < 8:
            print(grid[a][i], end="")
        else:
            print(grid[a][i])

I would like to thank y'all in advance.


回答1:


grid = [['.', '.', '.', '.', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['.', 'O', 'O', 'O', 'O', 'O'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['.', '.', '.', '.', '.', '.']]

for i in range(6):
    for a in range(9):
        if a < 8:
            print(grid[a][-(i+1)], end="")
        else:
            print(grid[a][i])

#output
....O....
...OOO...
..OOOOO..
.OOOOOOO.
.OOOOOOO.
..OO.OO..



回答2:


For a built-in solution ...

This little bit of code uses list transposition and reverse slicing:

list(map(list, zip(*grid)))[::-1]

Output:

....O....
...OOO...
..OOOOO..
.OOOOOOO.
.OOOOOOO.
..OO.OO..

Or, if you'd like to use numpy ...

Logic:

  • Create a numpy.ndarray from the grid.
  • Transpose the array and use reverse slicing to invert the transposed array.

Example:

import numpy as np

g = np.array(grid)
g.T[::-1]

Which creates an array as:

array([['.', '.', '.', '.', 'O', '.', '.', '.', '.'],
       ['.', '.', '.', 'O', 'O', 'O', '.', '.', '.'],
       ['.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.'],
       ['.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.'],
       ['.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.'],
       ['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.']], dtype='<U1')

And can be printed as:

for i in g.T[::-1]:
    print(''.join(i))

Output:

....O....
...OOO...
..OOOOO..
.OOOOOOO.
.OOOOOOO.
..OO.OO..

Comments:
I realise the course (most likely) encourages the use of lower-level Python constructs (list, dict, etc). And I wholly agree this is important / vital. However, I also feel it's important to expose those new to Python (and development in general) to powerful/efficient libraries such as numpy, and how they can be used. Because let's be honest, in the real world ... you're going to write a couple lines in numpy rather than 10 functions to accomplish the same task.




回答3:


A Easy understand way to complete it. (My English is not well, I hope you can understand.)

If it is hard for you to think which one to print, so to I. I usually create a reversed_grid array to save rotated result. The next thing to do is to find the relationship between and reversed_grid and grid after rotation. I generally look for some specific examples to summarize the general relationship between two arrays. For example, grid[0][0] will be stored in reversed_grid[5][0] after rotation. There are some other examples. ([0][5] -> [5][0] means grid[0][0] = reversed_grid[5][0])

[0][0] -> [5][0], [0][1] -> [4][0], [0][2] -> [3][0], ... , [0][5] -> [0][0]
[1][0] -> [5][1], [1][1] -> [4][1], [1][2] -> [3][1], ... , [1][5] -> [0][1]
[2][0] -> [5][2], ... 
[3][0] -> [5][3], ... 
... 
[8][0] -> [5][8], ...

Through the above examples, you can summarize the relationship is reversed_grid[6-j][i] = grid[i][j]. The last thing you have to do is print reversed_grid line by line.

The final code is as follows:

grid = [['.', '.', '.', '.', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['.', 'O', 'O', 'O', 'O', 'O'],
          ['O', 'O', 'O', 'O', 'O', '.'],
          ['O', 'O', 'O', 'O', '.', '.'],
          ['.', 'O', 'O', '.', '.', '.'],
          ['.', '.', '.', '.', '.', '.']]

reversed_grid = [['' for i in range(9)] for j in range(6)] # create and initialize reversed_grid
for row in range(9):
    for col in range(6):
        reversed_grid[5-col][row] = grid[row][col]

for row in range(6):
    for col in range(9):
        print(reversed_grid[row][col], end='')
    print()


来源:https://stackoverflow.com/questions/65004193/python-rotate-nested-lists-90

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!