How can I rotate this list of lists with python

I have a list list, and I need to rotate it ninety degrees. I managed to get the first new line, but I could not get any of them, so I put the for loop in the aa function, and I increased the count for each iteration, but I just had a big mess printed on the terminal.

grid = [['.', '.', '.', '.', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['0', '0', '0', '0', '.', '.'], ['0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '.'], ['0', '0', '0', '0', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] 

How do I need to turn.

 ''' ..00.00.. .0000000. .0000000. ..00000.. ...000... ....0.... ''' 

My code

 def get_row(grid, new_grid, new_row, count): for row in grid: new_row.append(row[count]) new_grid.append(new_row) new_grid = [] new_row = [] count = 0 for x in range(0, 6): count = 0 get_row(grid, new_grid, new_row, count) count +=1 for row in new_grid: print row 
+6
source share
4 answers

You want to rearrange the list of lists by making row columns and row columns into rows:

 grid = [['.', '.', '.', '.', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['0', '0', '0', '0', '.', '.'], ['0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '.'], ['0', '0', '0', '0', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] print("\n".join(map("".join,zip(*grid)))) 

Of:

 ..00.00.. .0000000. .0000000. ..00000.. ...000... ....0.... 

Or using a slightly different syntax with print_function , we can unzip with * and specify a separator with sep .:

 from __future__ import print_function print(*map("".join,zip(*grid)),sep="\n") 

Using python2, if you don't want to create proxy lists, you can use itertools so that it becomes the following:

 from __future__ import print_function from itertools import imap, izip print(*imap("".join,izip(*grid)),sep="\n") 

For completeness and to show you what your transposed list looks like:

 from pprint import pprint as pp pp(list(zip(*grid))) [('.', '.', '0', '0', '.', '0', '0', '.', '.'), ('.', '0', '0', '0', '0', '0', '0', '0', '.'), ('.', '0', '0', '0', '0', '0', '0', '0', '.'), ('.', '.', '0', '0', '0', '0', '0', '.', '.'), ('.', '.', '.', '0', '0', '0', '.', '.', '.'), ('.', '.', '.', '.', '0', '.', '.', '.', '.')] 

What if you really need lists instead of tuples, you could map back to the list:

 pp(list(map(list,zip(*grid)))) [['.', '.', '0', '0', '.', '0', '0', '.', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '.', '0', '0', '0', '0', '0', '.', '.'], ['.', '.', '.', '0', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '0', '.', '.', '.', '.']] 
+10
source

Index Magic:

 >>> grid = [['.', '.', '.', '.', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['0', '0', '0', '0', '.', '.'], ['0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '.'], ['0', '0', '0', '0', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] >>> for i in range(len(grid[0])): #assuming they all have the same length print (''.join(x[i] for x in grid)) ..00.00.. .0000000. .0000000. ..00000.. ...000... ....0.... 

or save to a new grid:

 >>> newgrid = [] >>> for i in range(len(grid[0])): #assuming they all have the same length newgrid.append([x[i] for x in grid]) >>> newgrid [['.', '.', '0', '0', '.', '0', '0', '.', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '.', '0', '0', '0', '0', '0', '.', '.'], ['.', '.', '.', '0', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '0', '.', '.', '.', '.']] 

or one line:

 >>> newgrid = [[x[i] for x in grid] for i in range(len(grid[0]))] >>> newgrid [['.', '.', '0', '0', '.', '0', '0', '.', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0', '0', '0', '.'], ['.', '.', '0', '0', '0', '0', '0', '.', '.'], ['.', '.', '.', '0', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '0', '.', '.', '.', '.']] 
+5
source

Actually, it’s easier to do this with a one-dimensional array than with a 2D array (editing: I was wrong, the zip approach is also quite simple. And the best IMO, leaving this for comparison), so just for fun, we'll move on to 1D, and then turn to the new 2D:

 >>> from itertools import chain >>> # Collapses to 1D >>> grid1d = list(chain(*grid)) >>> # Rotates and rebuilds as 2D >>> numcolumns = len(grid[0]) >>> rotated = [grid1d[i::numcolumns] for i in range(numcolumns)] >>> print(*rotated, sep="\n") ['.', '.', '0', '0', '.', '0', '0', '.', '.'] ['.', '0', '0', '0', '0', '0', '0', '0', '.'] ['.', '0', '0', '0', '0', '0', '0', '0', '.'] ['.', '.', '0', '0', '0', '0', '0', '.', '.'] ['.', '.', '.', '0', '0', '0', '.', '.', '.'] ['.', '.', '.', '.', '0', '.', '.', '.', '.'] 

Note: if you are using Python 2, this print syntax will require from __future__ import print_function .

+2
source

numpy.rot90 can do this quickly for large matrices such as images. Here is a quick example:

 import numpy as np grid = [['.', '.', '.', '.', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['0', '0', '0', '0', '.', '.'], ['0', '0', '0', '0', '0', '.'], ['.', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '.'], ['0', '0', '0', '0', '.', '.'], ['.', '0', '0', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] grid = np.rot90(grid, k=3) print grid # [['.' '.' '0' '0' '.' '0' '0' '.' '.'] # ['.' '0' '0' '0' '0' '0' '0' '0' '.'] # ['.' '0' '0' '0' '0' '0' '0' '0' '.'] # ['.' '.' '0' '0' '0' '0' '0' '.' '.'] # ['.' '.' '.' '0' '0' '0' '.' '.' '.'] # ['.' '.' '.' '.' '0' '.' '.' '.' '.']] 
+1
source

All Articles