473,394 Members | 1,769 Online

# 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 1324
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 7 by: 2mc | last post by: I am finding out all kinds of ways to do things in NumPy through the many suggestions I have received. It's exciting. Thanks to all who have replied on my other threads. I'm still having... 1 by: Gabriel Birke | last post by: Given the multidimensional list l: l = ] ] I want to access specific items the indices of which are stored in another list. For now, I created a function to do this: def getNestedValue(l,... 106 by: A | last post by: Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the... 2 by: Tim | last post by: Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?... 13 by: Kevin | last post by: Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I 2 by: d[ - - ]b | last post by: Hi there, Just wondering, if there is any way to have a dynamic / unspecified size of an array? It obviously needs to be updated, deleted etc. The only way I thought is by making the array... 2 by: BB | last post by: Hello, I have a HTML form containing multidimensional selects listing equipments and their quantitites. This allow the users to select the kind of equipment and quantitites they would like to... 16 by: Rehceb Rotkiv | last post by: Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb 7 by: krishna | last post by: What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no... 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: ryjfgjl | last post by: If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming... 0 by: ryjfgjl | last post by: In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files... 0 by: emmanuelkatto | last post by: Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel 1 by: Sonnysonu | last post by: This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to... 0 by: Hystou | last post by: There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2... 0 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can... 0 by: Hystou | last post by: Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows... 0 by: tracyyun | last post by: Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...