471,595 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

proposition for syntax for initialisation of multidimensional lists

i always have trouble explaining why creation of multidimensional lists
is not as straight forward as it could be in python. list comprehensions
are ugly if you are new to the language. i really would like to see it
made easy. i propose using tuples in the same way as integers are now.
example:
[0] * (2,3,4)

[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0,
0], [0, 0, 0, 0]]]

this is even less "mathematically bad" syntax than multiplication with
an integer, as multiplication of vectors is only well defined if they
are of the same length. [1] * 7 could be interpreted as a vector of
length one multiplied with a scalar 7 resulting in the vector [7]. (this
was not meant as a proposition to remove the current syntax.)

i would be easy to implement:

class mylist(list):
def __mul__(self, dims):
if dims.__class__ == (()).__class__:
if len(dims) > 1:
return [self.__mul__(dims[1:]) for i in range(dims[0])]
else:
return list.__mul__(self, dims[0])
else:
return list.__mul__(self, dims)

li = mylist()
li.append(0)
print li * (2,3,4)
what do you think?
klem fra nils
Jul 18 '05 #1
1 1262
Nils Grimsmo wrote:
i always have trouble explaining why creation of multidimensional lists
is not as straight forward as it could be in python.


Maybe because that is not really a sensible thing to do with the list
data structure, which can change in length after instantiation?
>>> [0] * (2,3,4) [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0,
0], [0, 0, 0, 0]]]

[SNIP]

what do you think?


I think I will continue doing this:
import numarray
numarray.zeros((2,3,4))

array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
--
Michael Hoffman
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gabriel Birke | last post: by
106 posts views Thread by A | last post: by
2 posts views Thread by d[ - - ]b | last post: by
16 posts views Thread by Rehceb Rotkiv | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.