Cheiron
Cheiron

Reputation: 3736

Inversing a twodimensional array in python

I have an array a, which is twodimensional. A contains objects which also contain objects. I want to make sure that a[1,1] becomes a[n,n], a[2,1] becomes a[n-1,n], a[2,2] becomes a[n-1][n-1] etc. I wrote the following code to do this:

tempArray = copy(self.topArea)
for y in range(0,len(tempArray)):
    for x in range(0,len(tempArray[y])):
        self.topArea[y][x] = tempArray[len(tempArray)-1-y][len(tempArray[y])-1-x]

But this achieves litteraly nothing. Deepcopying also does not help: the array does not get inverted.

How can I invert it?

Upvotes: 1

Views: 101

Answers (1)

mgilson
mgilson

Reputation: 310049

Do you want something like:

tempArray = [list(reversed(x)) for x in reversed(self.topArea)]

If everything is lists, you could also do:

tempArray = [x[::-1] for x in reversed(self.topArea)]

for a possible speed boost.

Upvotes: 3

Related Questions