1. Bruce Eckel
  2. Python 3 Patterns & Idioms
Issue #7 new

Comprehension / Nested Comprehensions / typo in identity matrix

Anonymous created an issue

There is a problem here:


In python we can represent such a matrix by a list of lists, where each sub-list represents a row. A 3 by 3 matrix would be represented by the following list: {{{

!python

[ [ 1, 0, 1 ], <- should be 1, 0, 0 [ 0, 1, 0 ], [ 0, 0, 1 ] ] }}}

The above matrix can be generated by the following comprehension: {{{

!python

[ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ] }}}


Comments (0)

  1. Log in to comment