[Tutor] Matrix

Alan Gauld ukc802591034 at btconnect.com
Thu Oct 27 00:53:03 CEST 2005


This doesn't seem to have been answered...

"Shi Mu" <samrobertsmith at gmail.com> wrote in message 
news:1d987df30510242126p331f2cfat1420f13c1c726ed1 at mail.gmail.com...
>I can not understand the use of "cell in row" for two times in the code:
>
> # convert the matrix to a 1D list
> matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
> items = [cell for row in matrix for cell in row]
> print items

Lets expand the list comprehension:

matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
items = []
for row in matrix:
     for cell in row:
          items.append(cell)
print items

Does that explain whats going on? Its just nesting another for loop.

HTH,


-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld






More information about the Tutor mailing list