[Baypiggies] One and two dim arrays in Python

Chris Rebert cvrebert at gmail.com
Tue Aug 26 02:44:17 CEST 2008


That's because you're using a dictionary rather than a list. The
dictionary doesn't care too much about the type of its keys and has no
concept of ordering or contiguousness, so you can add items to it
arbitrarily.

This is why another way to implement multidimensional arrays in Python
is to use a dictionary and tuples for the coordinates, e.g.:

a = {}
for x in range(x_size):
    for y in range(y_size):
        for z in range(z_size):
            coords = (x,y,z)
            a[coords] = something

- Chris
========
Follow the path of the Iguana...
Rebertia: http://rebertia.com
Blog: http://blog.rebertia.com



On Mon, Aug 25, 2008 at 5:00 PM, Nathan Pease <n8pease at yahoo.com> wrote:
> this seems to work:
> a = {}
> for i in range(0, 10): a[i] = 'foo'
> n
> On Aug 25, 2008, at 4:53 PM, David Elsen wrote:
>
> I have declared 'a' as :
> a=[] before the for loop. But it does not seem to help.
>
> On Mon, Aug 25, 2008 at 4:49 PM, David Elsen <elsen.david08 at gmail.com>
> wrote:
>>
>> Hi all,
>>
>> I would like to do something like following in Python:
>>
>> for i in range(0,10):
>>      a[i] = something
>>
>> just to initialize some values for 1d array.
>>
>> I am getting the error message 'a' is not defined.
>>
>> Is there a way to declare 1d or 2d arrays in Python?
>>
>> Thanks,
>> David
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>


More information about the Baypiggies mailing list