nikkitikitavi
nikkitikitavi

Reputation: 15

How to get rotated output of lists?

Could you help with an advice or redirect me to related topic.. I am new to python and programming and kinda stack here. I have to get the following output:

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

but instead I get:

......
.OO...
OOOO..
OOOOO.
.OOOOO
OOOOO.
OOOO..
.OO...
...... 

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(0, len(grid)):
  output = ""
  for j in range(0,len(grid[i])):
    output += str(grid[i][j])
  print(output)

Upvotes: 1

Views: 73

Answers (3)

jpp
jpp

Reputation: 164613

If you are permitted to use a 3rd party library, this can be done with numpy.rotate and setting k=-1 [i.e. 90% clockwise]:

import numpy as np

print(np.array(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 x in np.rot90(np.array(grid), k=-1):
    print(''.join(x))

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

Upvotes: -1

Alain T.
Alain T.

Reputation: 42133

You could use zip to traverse your grid in column order for printing :

for row in zip(*grid) : print("".join(row))

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

Note that this assumes that row 0 in your original grid represents column 0 in the transposed version. If you want a 90 degree rotation, then you will need to reverse the order of the row elements by using row[::-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 row in zip(*grid) : print("".join(row[::-1]))

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

Upvotes: 1

Joe Iddon
Joe Iddon

Reputation: 20414

You need to reverse the indexing to grid[j][i]. This also requires you to change your ranges to have the axis of the list line up (because your rows are now columns):

for i in range(len(grid[0])):
    output = ""
    for j in range(len(grid)):
        output += str(grid[j][i])
    print(output)

outputs:

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

You also didn't need to 0 for the start of the range because 0 is the default, as well as the indentation should be 4 spaces, so I corrected that too :)


This could also be done in a one-liner:

Either:

print('\n'.join(''.join(grid[j][i] for j in range(len(grid))) for i in range(len(grid[0]))))

Or:

[print(''.join(grid[j][i] for j in range(len(grid)))) for i in range(len(grid[0]))]

Upvotes: 2

Related Questions