-1

How can rotate multi-dimensional List without using the zip function, I have this list (but it can be longer):

testlist = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9],
           [10,11,12]]

I want to rotate it 90 degrees clockwise and also 90 degrees counterclockwise. ps: The reason i don't want to use zip is because this is a homework and i must write it in vanilla python .

jumanji
  • 11
  • 4
  • 2
    what do u mean with rotating it with 90 degrees clockwise – Cing Jan 18 '16 at 16:38
  • 6
    Why don't you want to use the `zip` function? It can be used towards a solution here. – Alex Riley Jan 18 '16 at 16:40
  • @ajcr It's probably part of a homework problem would be my guess as to the reason. Which is why all of the numpy answers also make me think they're not going to be what he wants. Whomever it's being turned in to probably wants vanilla python. – Jeff Langemeier Jan 18 '16 at 16:51
  • @Jeff Langemeier everything you just was completely right , its for a homework and it must be written in vanilla python ,i used the zip function and it worked but its not what im supposed to use . – jumanji Jan 18 '16 at 16:55
  • @MehdiMajoug So, what have you attempted, you're not going to learn much from us doing it for you. – Jeff Langemeier Jan 18 '16 at 16:57
  • Show us what code you have tried thus far. – Panda Jan 18 '16 at 16:58

4 Answers4

2

Of course, the proper way to rotate the multi-dimensional list would be to use zip on the reversed list. I assume you've already found this in other questions here:

>>> testlist = [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9],
                [10,11,12]]

>>> list(zip(*testlist[::-1]))
[[10, 7, 4, 1], [11, 8, 5, 2], [12, 9, 6, 3]]

If you do not want to do that (and also don't want other builtin functions), you'll basically have to replicate the behaviour of zip. In a very simple form (e.g. just assuming that all the lists have the same length) you can do this in a nested list comprehension. Remember to rotate the list, too:

>>> [[x[i] for x in testlist[::-1]] for i in range(len(testlist[0]))]
[[10, 7, 4, 1], [11, 8, 5, 2], [12, 9, 6, 3]]

Obviously, using zip is much clearer and less error-prone. Of course, you could also spread this out to a few lines using two nested loops. Doing the same for 90 degrees counter-clockwise is left as an excercise to the reader.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

You code would be like:

>>> import numpy as np
>>> s = np.array([[1,2,3], [4,5,6], [7,8,9]], int)
>>> s
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> np.rot90(s)
array([[3, 6, 9],
       [2, 5, 8],
       [1, 4, 7]])
>>> np.rot90(s, 3) # Rotate counterclockwise 3 times
array([[9, 8, 7],
       [6, 5, 4],
       [3, 2, 1]])
George Petrov
  • 2,729
  • 1
  • 13
  • 20
0

I am assuming you mean transpose.

How about this?

import numpy as np
a=np.array(testlist)
print a.T #this is a view
NinjaGaiden
  • 3,046
  • 6
  • 28
  • 49
0

Since you've stated it's for homework, I'll give you the steps, but not the code.

Rotate 90

  1. Each row becomes a column (you can use 2 for loops and enumerate)
  2. Reverse order of the rows (you can use slices with a negative step)

Rotate -90

  1. Each row from Rotate 90 becomes a column (2 for loops and enumerate)
  2. Reverse order of the rows (slice with a negative step)
Jeff Langemeier
  • 1,018
  • 1
  • 10
  • 21