[Tutor] Table like array in Python

Michael Connors connorsml at gmail.com
Wed Mar 26 10:56:25 CET 2008


On 26/03/2008, Gloom Demon <gloomdemon at gmail.com> wrote:
>
> Hello :-)
>
> Example (cost of something in different countries by different years)
>
> Record1 US 2006 22.10
> Record2 US 2007 23.45
> Record3 UK 2007 22.90
> ..................................
> <http://mail.python.org/mailman/listinfo/tutor>
>
>
In Python a list is similiar to an array in Pascal.
You define a list like this: my_list = [1, 2, 3, 4]

However I dont think you want an array here, if I was doing something like
this I would use a dictionary. A dictionary stores keys and values.
So you could do something like this:

records = {'US': {'2007': 22.5, '2008': 44.8}, 'UK': {'2008': 3.4, '2007':
2.6}}

You can now access a particular record as follows:

In: print records['UK']['2007']
Out: 2.6

Regards,
-- 
Michael Connors
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080326/c9fe40eb/attachment-0001.htm 


More information about the Tutor mailing list