Sterling Butters Sterling Butters - 7 days ago 6
Python Question

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!!

Answer

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...

Comments