Skip to content Skip to sidebar Skip to footer

Python: Rotate Nested Lists -90°

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

Solution 1:

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 npg= 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.

Solution 2:

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 inrange(6):
    for a inrange(9):
        if a < 8:
            print(grid[a][-(i+1)], end="")
        else:
            print(grid[a][i])

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

Solution 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 inrange(9)] for j inrange(6)] # createand initialize reversed_grid
forrowinrange(9):
    for col inrange(6):
        reversed_grid[5-col][row] = grid[row][col]

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

Post a Comment for "Python: Rotate Nested Lists -90°"