Reverse certain elements in a 2d array to produce a matrix in the specified format, Python 3

Question:

I have the following code for a list of lists with the intention of creating a matrix of numbers:

grid=[[1,2,3,4,5,6,7],[8,9,10,11,12],[13,14,15,16,17],[18,19,20,21,22]]

On using the following code which i figured out would reverse the list, it produces a matrix …

for i in reversed(grid):
    print(i)

The output is:

[18, 19, 20, 21, 22]
[13, 14, 15, 16, 17]
[8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7]

I want however, the output to be as below, so that the numbers “connect” as they go up:

[22,21,20,19,18]
[13,14,15,16,17]
[12,11,10,9,8]
[1,2,3,4,5,6,7]

Also, for an upvote, I’d be interested in more efficient ways of generating the matrix in the first place. For instance, to generate a 7×7 array – can it be done using a variable, for instance 7, or 49. Or for a 10×10 matrix, 10, or 100?

UPDATE:
Yes, sorry – the sublists should all be of the same size. Typo above

UPDATE BASED ON ANSWER BELOW

These two lines:

>>> grid=[[1,2,3,4,5,6,7],[8,9,10,11,12],[13,14,15,16,17],[18,18,20,21,22]]
>>> [lst[::-1] for lst in grid[::-1]]

produce the following output:

[[22, 21, 20, 18, 18], [17, 16, 15, 14, 13], [12, 11, 10, 9, 8], [7, 6, 5,  4, 3, 2, 1]]

but I want them to print one line after the other, like a matrix ….also, so I can check the output is as I specified. That’s all I need essentially, for the answer to be the answer!

Asked By: user7500711

||

Answers:

You need to reverse the list and also the sub-lists:

[lst[::-1] for lst in grid[::-1]]

Note that lst[::-1] reverses the list via list slicing, see here.

You can visualize the resulting nested lists across multiples lines with pprint:

>>> from pprint import pprint
>>> pprint([lst[::-1] for lst in grid[::-1]])
[[22, 21, 20, 19, 18],
 [17, 16, 15, 14, 13],
 [12, 11, 10, 9, 8],
 [7, 6, 5, 4, 3, 2, 1]]
Answered By: Chris_Rands

usually 2D matrices are created, manipulated with numpy

then index slicing can reorder rows, columns

import numpy as np

def SnakeMatrx(n):
    Sq, Sq.shape = np.arange(n * n), (n, n) # Sq matrix filled with a range
    Sq[1::2,:] = Sq[1::2,::-1] # reverse odd row's columns
    return Sq[::-1,:] + 1    # reverse order of rows, add 1 to every entry

SnakeMatrx(5)
Out[33]: 
array([[21, 22, 23, 24, 25],
       [20, 19, 18, 17, 16],
       [11, 12, 13, 14, 15],
       [10,  9,  8,  7,  6],
       [ 1,  2,  3,  4,  5]])

SnakeMatrx(4)
Out[34]: 
array([[16, 15, 14, 13],
       [ 9, 10, 11, 12],
       [ 8,  7,  6,  5],
       [ 1,  2,  3,  4]])

if you really want a list of lists:

SnakeMatrx(4).tolist()
Out[39]: [[16, 15, 14, 13], [9, 10, 11, 12], [8, 7, 6, 5], [1, 2, 3, 4]]

numpy is popular but not a official Standard Library in Python distributions

of course it can be done with list manipulation

def SnakeLoL(n):
    Sq = [[1 + i + n * j for i in range(n)] for j in range(n)] # Sq LoL filled with a range
    for row in Sq[1::2]:
        row.reverse()     # reverse odd row's columns
    return Sq[::-1][:]    # reverse order of rows
# or maybe more Pythonic for return Sq[::-1][:]
#     Sq.reverse()   # reverse order of rows 
#     return Sq 

SnakeLoL(4)
Out[91]: [[16, 15, 14, 13], [9, 10, 11, 12], [8, 7, 6, 5], [1, 2, 3, 4]]

SnakeLoL(5)
Out[92]: 
[[21, 22, 23, 24, 25],
 [20, 19, 18, 17, 16],
 [11, 12, 13, 14, 15],
 [10, 9, 8, 7, 6],
 [1, 2, 3, 4, 5]]

print(*SnakeLoL(4), sep='n')
[16, 15, 14, 13]
[9, 10, 11, 12]
[8, 7, 6, 5]
[1, 2, 3, 4]
Answered By: f5r5e5d

Simple way of python:

list(map(lambda i: print(i), [lst[::-1] for lst in grid[::-1]]))
Answered By: Saurav Sharma
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.