Multi-dimensional list initialization
joshua.landau.ws at gmail.com
Wed Nov 7 14:39:57 CET 2012
On 7 November 2012 11:11, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On Nov 7, 2012 5:41 AM, "Gregory Ewing" <greg.ewing at canterbury.ac.nz>
> > If anything is to be done in this area, it would be better
> > as an extension of list comprehensions, e.g.
> > [[None times 5] times 10]
> > which would be equivalent to
> > [[None for _i in xrange(5)] for _j in xrange(10)]
> I think you're right that the meaning of list-int multiplication
> can't/shouldn't be changed if this way.
> A multidimensional list comprehension would be useful even for people who
> are using numpy as it's common to use a list comprehension to initialise a
> numpy array.
> A more modest addition for the limited case described in this thread could
> be to use exponentiation:
> >>>  ** (2, 3)
> [[0, 0, 0], [0, 0, 0]]
Hold on: why not just use multiplication?
>>>  * (2, 3)
is an error now, and it makes total sense. Additionally, it's not breaking
the "no copy -- _ever_" rule because none of the lists existed before. The
values inside the list would be by reference, as before, so lst * (x,)
would be the same as lst * x if x is an integer.
*I* would use this a lot. This is the first thing on this thread that makes
a lot of sense to me.
We do have to think of the potential problems, though. There are definitely
some. For one, code that relies on lst * x throwing an error would break.
It may confuse others - although I don't see how.
But I don't see any big problems, so I really do like this idea.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list