Transforming a matrix by placing its elements in the reverse order of the initial in python

Question:

im looking to solve the problem mentioned in the title without any specific functions that may or may not exist for this[Example]. Something along the lines of using mostly loops.

I tought about reversing each individual row using list.reverse() and then moving the rows around but im not sure how to implement it.

Asked By: Gui Pinto

||

Answers:

Try This

Matrix = [[1,2,3],[4,5,6],[7,8,9]]
newMatrix = []
for line in range(len(Matrix)):
    Matrix[line].reverse()
    newMatrix.append(Matrix[line])

newMatrix.reverse()
print(newMatrix)

output

[[9, 8, 7], [6, 5, 4], [3, 2, 1]]
Answered By: Exile

try this new realization and this will not change the original Matrix

Matrix = [[1,2,3],[4,5,6],[7,8,9]]
newMatrix = []
for line in range(len(Matrix)):
    newMatrix.append(sorted(Matrix[line],reverse=True))

newMatrix.reverse()
print(newMatrix)
print(Matrix)

output:

[[9, 8, 7], [6, 5, 4], [3, 2, 1]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Answered By: Exile
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.