Multidimensional arrays - how?

Fernando Pérez fperez528 at yahoo.com
Fri May 3 17:47:16 EDT 2002


delete .PL wrote:

> Yes , but what if I would like to use string keys instead of numbers?
> E.g.
> 
> book, chapter, para = 'a', 'b', 'c' ?
> 
> x = [[['para0', 'para1']]] # will not work as I wanted
> 
> Nested lists are easer implemented in PHP, I am afraid.
> $x['a']['b']['c'] = 'txt' works without any problem.

No, it's just that in Python such a thing (a data structure indexable by 
string) is called a dictionary, not a list:

In [3]: d={'a':{'b':{}}}

In [4]: d['a']['b']['c']='txt'

In [5]: d
Out[5]: {'a': {'b': {'c': 'txt'}}}

A dict can be arbitrarily nested, and its keys can be any hashable object 
(superficially, numbers, strings and tuples of such).

Nothing wrong with the language or php's superiority, simply that in python 
things are done in a certain way (like everywhere else). Once you learn each 
language's idioms (true for _any_ language) things will feel natural, until 
then your background will make certain things feel 'hard'. It's the normal 
process of learning anything new.

cheers,

f.



More information about the Python-list mailing list