1

I need to rotate this 2 dimensional array clockwise and counter clockwise.

LIST = [['.', '.', '.', '.', '.', '.'],
        ['.', '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', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

I tried this to rotate the list clockwise but it doesn't give me any results:

rotated_list = []

i = 0
j = 0
x = len(LIST)
y = len(LIST[0])

while i < x:
    while j < y:
        rotated_list[j][i] = LIST[i][j]
        j += 1
    i += 1

The result I need should look something like this:

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

2 Answers 2

0

The problem is that you're trying to set a value of what you can imagine being a non-existent place. The list is not an array, it doesn't have any empty cells, instead, you have to l.append(value) to it. Also, you can use for loops instead of while - they do the same thing. Here's my solution:

rotated_list = []

x = len(LIST[0])
y = len(LIST)

for i in range(x):
    rotated_list.append([])
    for j in range(y):
        rotated_list[-1].append(LIST[j][i])

Hope that's helpful!

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to get back to you so late, I tried this and it worked. Thank you!
0

You can use zip in a list comprehension to do the rotation:

LIST = [['.', '.', '.', '.', '.', '.'],
        ['.', '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', '.', '.', '.'],
        ['.', '.', '.', '.', '.', 'X']]

Rotate once:

LIST = [ [*r][::-1] for r in zip(*LIST) ]

for line in LIST: print(line)

['.', '.', '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', '.', '.', '.']
['X', '.', '.', '.', 'O', '.', '.', '.', '.']

Rotate again:

LIST = [ [*r][::-1] for r in zip(*LIST) ]

for line in LIST: print(line)

['X', '.', '.', '.', '.', '.']
['.', '.', '.', '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', '.']
['.', '.', '.', '.', '.', '.']

Rotate again:

LIST = [ [*r][::-1] for r in zip(*LIST) ]

for line in LIST: print(line)

['.', '.', '.', '.', 'O', '.', '.', '.', 'X']
['.', '.', '.', '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 counterclockwise rotation you can use this:

LIST = [ [*r] for r in zip(*map(reversed,LIST)) ]

Without list comprehensions:

LIST = list(map(list,map(reversed,zip(*LIST)))) # clockwise
LIST = list(map(list,zip(*map(reversed,LIST)))) # counter clockwise

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.