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[]. 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.
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]]
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]]
im looking to solve the problem mentioned in the title without any specific functions that may or may not exist for this[]. 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.
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]]
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]]