[Python-ideas] Python list of list matrixes without NumPy

Juancarlo Añez apalala at gmail.com
Fri Apr 5 22:05:11 EDT 2019


The semantics of list operators are congruent, but unintuitive sometimes.
For example, I've fallen over this many times:

>>> truth_matrix = [[False] * n ] * m


It doesn't create what you'd want.

The only safe way I know to creat an NxM matrix is:

>>> truth_matrix = [ [False for _ in range(n) for _ in range(m)]

There could be a library to deal with that common case, and those of
checking if a list of lists (of lists?) is properly rectangular, square,
etc., and several other common cases. It takes  experience to be aware that
`truth_matrix[i][j]` will change more than one "cell" if the initialization
is the former.

`numpy` is not part of stdlib. A standard library for list of list would be
usefu, specially to newcomersl. I don't remember such being discussed
lately.

>>> truth_matrix = lol(n, m, init=bool)

>>> other_matrix = lol(n, m, o, p, init=float)


Maybe it could also be done with syntax, but I don't have any ideas in that
regard (I don't think "lol()" is overloaded).

Regards,

-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190405/4e945960/attachment.html>


More information about the Python-ideas mailing list