[Python-ideas] Construct a matrix from a list: matrix multiplication

Franklin? Lee leewangzhong+python at gmail.com
Sat Apr 1 00:33:21 EDT 2017


On Fri, Mar 31, 2017 at 3:58 AM, Chris Angelico <rosuav at gmail.com> wrote:
> This keeps on coming up in one form or another - either someone
> multiplies a list of lists and ends up surprised that they're all the
> same, or is frustrated with the verbosity of the alternatives.
>
> Can we use the matmul operator for this?

In math, a number can be considered a 1-dimensional vector. You can
multiply a 1-dimensional vector by an n-dimensional vector _as
matrices_ to get the same result as scalar multiplication. Using it as
a deep-copy multiplication operator might make things confusing when
someone moves to or from Numpy.

Instead of using multiplication to create homogenous lists, how about
using the list constructor?
    list(0, 4) #list of 4 elements, all 0
    list(0, (4,4)) #4x4

Concerns with my idea:
1. Someone might expect `list(0, 4)` to return `[0, 4]`.
2. Someone might want to write `list(list(0, 4), 4)`, which has the
same issue as `[[0] * 4] * 4`.
3. Contrast with the `bytes` constructor. `bytes(n) == b'\0' * 10`

The `dict` constructor has a `.fromkeys` class method. An analogous
method for `list` can be `list.withshape(4,4, val=0)`. That's likely
to be too big a change to solve a small problem.


More information about the Python-ideas mailing list