Array design question

Aurelio Martin Massoni amartin at traza-si.com
Thu May 29 06:26:57 EDT 2003


"Peter Slizik" <peter.slizik at pobox.sk> escribió en el mensaje
news:bb4ljk$5pvsi$1 at ID-196014.news.dfncis.de...
>
> Hi pythoners,
>
>    after changing some messages in the 'Two dimensional array' thread,
> the discussion turned into somewhat philosophical debate on the way
> Python works with arrays.
>
>    I've been working with languages like PHP and Perl for some time.
> Things so simple in PHP looks very difficult in Python (at least for
me).
>
> Simple PHP code
>
> a[1] = 'aaa'
> a[2] = 'bbb'
> a[3] = 'ccc'
>
> will in Python read
>
> a.append('aaa')
> a.append('bbb')
> a.append('ccc')
>
> which is in my opinion uglier than PHP code.

Try this in Python :

a = {}
a[1] = 'aaa'
a[2] = 'bbb'
a[3] = 'ccc'


> Simple PHP code
>
> while( !eof() ) {
>      line = readline();
>      (number, text) = split(line);
>      array[number] = text;
> }

In Python:

array = {}
for line in file.xreadlines():
        number, text = line.split()
        array[ number ] = text







More information about the Python-list mailing list