Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix Mirroring in python

I have a matrix of numbers:

[[a, b, c] 
 [d, e, f] 
 [g, h, i]]

that I would like to be mirrored accordingly:

[[g, h, i]
 [d, e, f]
 [a, b, c] 
 [d, e, f] 
 [g, h, i]]

And then again to yield:

[[i, h, g, h, i]
 [f, e, d, e, f]
 [c, b, a, b, c] 
 [f, e, d, e, f] 
 [i, h, g, h, i]]

I would like to stick to basic Python packages like numpy. Thanks in advance for any help!!

like image 634
Sterling Butters Avatar asked Nov 28 '16 04:11

Sterling Butters


1 Answers

This can be accomplished using a simple helper function in pure python:

def mirror(seq):
    output = list(seq[::-1])
    output.extend(seq[1:])
    return output

inputs = [
   ['a', 'b', 'c'],
   ['d', 'e', 'f'],
   ['g', 'h', 'i'],
]
print(mirror([mirror(sublist) for sublist in inputs]))

Obviously, once the mirrored list is created, you can use it to create a numpy array or whatever...

like image 112
mgilson Avatar answered Oct 16 '22 20:10

mgilson