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

15 views
Skip to first unread message

Juancarlo Añez

unread,
Apr 5, 2019, 10:06:30 PM4/5/19
to Python-Ideas
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

Christopher Barker

unread,
Apr 6, 2019, 2:49:53 PM4/6/19
to Juancarlo Añez, python-ideas
By the way, your reply isn't go to the list --bringing it back on.

On Sat, Apr 6, 2019 at 5:03 AM Juancarlo Añez <apa...@gmail.com> wrote:
Or maybe there is one in PyPi — have you looked? 

There's a bunch of "matrix" packages in PyPi, none of them dealing with the simple use cases I'm talking about.

Be careful about terminology -- "matrix" is a term from mathematics (linear algebra) that has a specific meaning -- I don't think that's what you mean. If it is what you mean, then what you want is numpy (though it fact, numpy is not a matrix system -- it is general purpose n-dimensional arrays designed for numerical computation that includes some features to support  linear algebra ).
 
It could be static methods in `list`?

x = list.matrix(n, m, default=0.0)
y = list.matrix(n, m, o, p, default=False)
i = list.eye(n, False, True) 

If all you want are constructors for common 2-d or n-d arrays that would create lists of lists, then make a handful of utility functions, put them on PyPi, and see if anyone finds them useful -- if so, then offer it up as an addition the standard list object.

But for my part, I think simple constructors are of limited utility -- sure, it's an easy source of errors to do it "wrong", e.g.:

[[None] * 5] * 6 appears to create a 2 dimensional array, but really has all the rows as references to the same object. But users will discover this pretty quickly, and there is something to be said for folks needing to understand how python works with regard to multiple references to the same mutable object.

But what might be more useful is a 2D or ND array class that would provide more than just constructors, but would provide nifty things like 2-dimension indexing:

arr = NdArray((3,4), fill=None)

arr:

[[None, None, None, None],
 [None, None, None, None],
 [None, None, None, None]]

and nifty things like 2-d indexing:

arr[:, 2] to get columns, for instance.

Also some controls on resizing -- you really don't want folks able to append arbitrarily to  any of this.

THAT might gain some traction.

(or not, as you get this with numpy, and so much more, anyway.

But either way, write it and show its utility first.

-CHB





 
--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython

Juancarlo Añez

unread,
Apr 6, 2019, 3:51:14 PM4/6/19
to Steve Barnes, Python-Ideas


On Sat, Apr 6, 2019 at 4:11 AM Steve Barnes <gadge...@live.co.uk> wrote:

> ipython

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]

Type 'copyright', 'credits' or 'license' for more information

IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

 

In [1]: [[False]*2]*5

Out[1]:

[[False, False],

[False, False],

[False, False],

[False, False],

[False, False]]

# Looks like exactly what was wanted


[ins] In [1]: x = [[False]*2]*5                                                                                 

[ins] In [2]: x                                                                                                 
Out[2]: 
[[False, False],
 [False, False],
 [False, False],
 [False, False],
 [False, False]]

[ins] In [3]: x[1][1] = True                                                                                    

[ins] In [4]: x                                                                                                 
Out[4]: [[False, True], [False, True], [False, True], [False, True], [False, True]]

--
Juancarlo Añez
Reply all
Reply to author
Forward
0 new messages