Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Define a 2d Array?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jillian.calderon at gmail

Oct 11, 2008, 7:19 PM

Post #1 of 7 (18743 views)
Permalink
Define a 2d Array?

Hi.

How do I define a 2d list?

For instance, to define a 4 by 5 list, I wanted to do this:
n=4
m=5
world = [n][m]
However, it gives me an invalid syntax error saying the index is out
of range.
I know this is a real newbie question, but if you could help me out,
I'd really appreciate it!

Thanks,
Jill
--
http://mail.python.org/mailman/listinfo/python-list


skip at pobox

Oct 11, 2008, 7:30 PM

Post #2 of 7 (18691 views)
Permalink
Re: Define a 2d Array? [In reply to]

Jill> How do I define a 2d list?

Python doesn't truly have 2d lists in the way you might think of 2d arrays
in C or Fortran. It has 1d lists which can contain any Python object,
including other lists. If you wanted to create a 4x5 list you'd do
something like this:

N = 4
M = 5
mylist = []
for i in range(N):
mylist.append([0.0] * M)

If you are looking to do numeric work with such multidimensional lists you
should consider the builtin array object or the numpy package:

http://docs.python.org/dev/library/array.html#module-array
http://numpy.scipy.org/

Skip
--
http://mail.python.org/mailman/listinfo/python-list


Eric_Dexter at msn

Oct 12, 2008, 7:49 AM

Post #3 of 7 (18678 views)
Permalink
Re: Define a 2d Array? [In reply to]

On Oct 11, 9:30 pm, s...@pobox.com wrote:
>     Jill> How do I define a 2d list?
>
> Python doesn't truly have 2d lists in the way you might think of 2d arrays
> in C or Fortran.  It has 1d lists which can contain any Python object,
> including other lists.  If you wanted to create a 4x5 list you'd do
> something like this:
>
>     N = 4
>     M = 5
>     mylist = []
>     for i in range(N):
>         mylist.append([0.0] * M)
>
> If you are looking to do numeric work with such multidimensional lists you
> should consider the builtin array object or the numpy package:
>
>    http://docs.python.org/dev/library/array.html#module-array
>    http://numpy.scipy.org/
>
> Skip

I think you can do

mylist = [[]] or somesuch...

if you are looking on google for examples you will comonly find them
in spreadsheets.. I have one in the editor part of dex tracker
(available on source forge) The array will start at zero and ie x[0]
and will keep growing as long as you .append it.. You don't define
the size in advance like you would with other languages.. You need to
have values befour you try to
use a location in the array.
--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Oct 12, 2008, 2:38 PM

Post #4 of 7 (18675 views)
Permalink
Re: Define a 2d Array? [In reply to]

On Oct 12, 1:30 pm, s...@pobox.com wrote:
>     Jill> How do I define a 2d list?

>
> If you are looking to do numeric work with such multidimensional lists you
> should consider the builtin array object or the numpy package:
>
>    http://docs.python.org/dev/library/array.html#module-array

The built-in array module does *NOT* support multidimensional arrays.

The referenced docs say (first two sentences) "This module defines an
object type which can compactly represent an array of basic values:
characters, integers, floating point numbers. Arrays are sequence
types and behave very much like lists, except that the type of objects
stored in them is constrained."

"behave very much like lists" is in no way to be construed as
supporting multiple dimensions, and that type constraint means that
you can't even have an array of arrays.

However you can have a list of arrays; this can be a memory-efficient
solution for some 2D applications. Note that arrays are not
recommended for CPU efficiency, as (in general) each time you access
an array element, a new Python object must be created. Escape clause:
CPython and single-byte arrays (type 'c' and some values of types 'b'
and 'B').

Cheers,
John
--
http://mail.python.org/mailman/listinfo/python-list


skip at pobox

Oct 12, 2008, 3:43 PM

Post #5 of 7 (18677 views)
Permalink
Re: Define a 2d Array? [In reply to]

Eric> I think you can do

Eric> mylist = [[]] or somesuch...

That won't do what you want. You've defined a list with a single element
(another list). You might have been thinking of something like this:

>>> N = 4
>>> M = 5
>>> mylist = [[0.0] * M] * N

While to the casual glance it looks like what you want:

>>> print mylist
[[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]

assigning to an element of this structure demonstrates its shortcoming:


>>> mylist[1][1] = 42.7
>>> print mylist
[[0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0], [0.0, 42.700000000000003, 0.0, 0.0, 0.0]]

There is just one copy of [0.0] * M referenced N times.

Skip
--
http://mail.python.org/mailman/listinfo/python-list


thomas.p.krauss at gmail

Oct 12, 2008, 6:04 PM

Post #6 of 7 (18671 views)
Permalink
Re: Define a 2d Array? [In reply to]

On Oct 11, 9:19 pm, Jillian Calderon <jillian.calde...@gmail.com>
wrote:
> How do I define a 2d list?
>
> For instance, to define a 4 by 5 list, I wanted to do this:
> n=4
> m=5
> world = [n][m]
> However, it gives me an invalid syntax error saying the index is out
> of range.

Here are some examples of how you can use list comprehensions to do
this:

In [1]: n=4

In [2]: m=5

In [3]: world = [[[] for ni in range(n)] for mi in range(m)]

In [4]: world
Out[4]:
[[[], [], [], []],
[[], [], [], []],
[[], [], [], []],
[[], [], [], []],
[[], [], [], []]]

In [5]: world[0][0]
Out[5]: []

In [6]: len(world)
Out[6]: 5

In [7]: len(world[0])
Out[7]: 4

In [8]: world = [[[ni+mi*n] for ni in range(n)] for mi in range(m)]

In [9]: world
Out[9]:
[[[0], [1], [2], [3]],
[[4], [5], [6], [7]],
[[8], [9], [10], [11]],
[[12], [13], [14], [15]],
[[16], [17], [18], [19]]]

Best,
Tom
--
http://mail.python.org/mailman/listinfo/python-list


jillian.calderon at gmail

Oct 12, 2008, 7:18 PM

Post #7 of 7 (18680 views)
Permalink
Re: Define a 2d Array? [In reply to]

Thanks, everyone. All of your help solved at least my data storage
problem!
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.