0

How to to make this array rotate by 90 degrees to right without using numpy.

multiarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
auxiliaryArray = []
colLength = len(multiarray[0])
rowLength = len(multiarray)
for indexRow in range(len(multiarray)):
    for indexCol in range(len(multiarray[0])):
        auxiliaryArray[indexCol][rowLength - 1 - indexRow] = multiarray[indexRow][indexCol]

print(auxiliaryArray)

Error: IndexError: list index out of range

Desired Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Zukashi
  • 31
  • 7
  • instead of making an empty `list`, initialize a `list` with zeroes or some other values, that way you can't get out of range error – Mahrkeenerh Nov 04 '21 at 16:49
  • if you are looking at mathematic way without using additional library you can read this. https://integratedmlai.com/matrixinverse/ – Shahin Shirazi Nov 04 '21 at 16:53

2 Answers2

0

You can use zip on the reversed array:

auxiliaryArray = list(zip(*multiarray[::-1]))

or

auxiliaryArray = list(zip(*reversed(multiarray)))

output: [(7, 4, 1), (8, 5, 2), (9, 6, 3)]

If you need lists and not tuples:

auxiliaryArray = list(map(list, zip(*reversed(multiarray))))

output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

mozway
  • 194,879
  • 13
  • 39
  • 75
0

This does it:

list(zip(*multiarray[::-1]))