2

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 7x7 array - can it be done using a variable, for instance 7, or 49. Or for a 10x10 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!

3 Answers3

1

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]]
Community
  • 1
  • 1
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • could you explain what lst[::-1] does please? Also, I just made an edit above, in terms of how to print this so it is like a matrix. –  Feb 01 '17 at 16:22
  • Thanks Chris_Rands. However, the pprint command doesn't quite work - what does it do any way? SyntaxError: multiple statements found while compiling a single statement –  Feb 01 '17 at 16:49
  • @PythonFish It does work, are you sure you copied my command correctly? See: https://docs.python.org/3/library/pprint.html – Chris_Rands Feb 01 '17 at 16:53
  • Thanks. Here's a follow on question, if you have any immediate answers! http://stackoverflow.com/questions/41985139/turning-in-an-item-in-a-2d-array-a-specific-colour-based-on-user-input –  Feb 01 '17 at 16:55
0

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]
f5r5e5d
  • 3,656
  • 3
  • 14
  • 18
  • is there a reason import numpy as np fails to work on IDLE -ImportError: No module named 'numpy'. Also, as I'm developing for student samples, I don't really want to use numpy in the first place. Really, as the question says, I'd like the quickest way to generate a list of lists (columns and rows) for a specified matrix e.g. 7x7, and display it on the screen as shown –  Feb 01 '17 at 18:06
  • @PythonFish added a "pure python" version, how to print one row of List of Lists per line – f5r5e5d Feb 01 '17 at 19:13
  • Thanks -is there a way to ensure the OUTPUT is evenly laid out so it looks like a matrix (even number of spaces for the bottom row that contain only one integer). ALSO, your version above, on printing, doesn't produce a matrix (output), but a long list. How can I get it to print out visually like a matrix –  Feb 02 '17 at 07:52
  • @PythonFish, I'm not up on formatting but lots of options exist https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=python+print+matrix+align+columns – f5r5e5d Feb 02 '17 at 08:13
0

Simple way of python:

list(map(lambda i: print(i), [lst[::-1] for lst in grid[::-1]]))
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 21 '22 at 12:10