Tags: array, arraya1010is, basic, define, defining, dimensional, multi, programming, python

defining multi dimensional array

On Programmer » Python

7,167 words with 8 Comments; publish: Wed, 07 May 2008 19:56:00 GMT; (20093.75, « »)

hi...

basic question..

how do i define a multi dimensional array

a[10][10]

is there a kind of a = array(10,10)

thanks

-bruce

All Comments

Leave a comment...

  • 8 Comments
    • update...

      i need a multi dimensional array of lists...

      ie

      [q,a,d]

      [q1,a1,d1]

      [q2,a2,d2]

      [q3,a3,d3]

      which would be a (3,4) array...

      --Original Message--

      From: python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org

      [mailto:python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org]On Behalf

      Of bruce

      Sent: Tuesday, July 04, 2006 8:15 PM

      To: python-list.python.todaysummary.com.python.org

      Subject: defining multi dimensional array

      hi...

      basic question..

      how do i define a multi dimensional array

      a[10][10]

      is there a kind of a = array(10,10)

      thanks

      -bruce

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

      #1; Wed, 07 May 2008 19:58:00 GMT
    • bruce wrote:

      > basic question..

      > how do i define a multi dimensional array

      > a[10][10]

      > is there a kind of a = array(10,10)

      It's just a list of lists.

      Erik Max Francis && max.python.todaysummary.com.alcyone.com && http://www.alcyone.com/max/

      San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis

      Have you ever loved somebody / Who didn't know

      -- Zhane

      #2; Wed, 07 May 2008 19:59:00 GMT
    • i tried to do

      a1[10][10] = ['a','q']

      and get an error saying a1 is not defined...

      -bruce

      --Original Message--

      From: python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org

      [mailto:python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org]On Behalf

      Of Erik Max Francis

      Sent: Tuesday, July 04, 2006 9:14 PM

      To: python-list.python.todaysummary.com.python.org

      Subject: Re: defining multi dimensional array

      bruce wrote:

      > basic question..

      > how do i define a multi dimensional array

      > a[10][10]

      > is there a kind of a = array(10,10)

      It's just a list of lists.

      Erik Max Francis && max.python.todaysummary.com.alcyone.com && http://www.alcyone.com/max/

      San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis

      Have you ever loved somebody / Who didn't know

      -- Zhane

      --

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

      #3; Wed, 07 May 2008 20:00:00 GMT
    • bruce wrote:

      > hi...

      > basic question..

      > how do i define a multi dimensional array

      > a[10][10]

      I find that list comprehensions are useful for this.

      [ [None for x in xrange(10)] for y in xrange(10)]

      You could easily write a wrapper for it to clean the syntax a bit.

      #4; Wed, 07 May 2008 20:01:00 GMT
    • do i need to import anything for this.. or is it supposed to work out of the

      box..

      and just what is it doing!!!!!

      -bruce

      --Original Message--

      From: python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org

      [mailto:python-list-bounces+bedouglas=earthlink.net.python.todaysummary.com.python.org]On Behalf

      Of tac-tics

      Sent: Tuesday, July 04, 2006 9:53 PM

      To: python-list.python.todaysummary.com.python.org

      Subject: Re: defining multi dimensional array

      bruce wrote:

      > hi...

      > basic question..

      > how do i define a multi dimensional array

      > a[10][10]

      I find that list comprehensions are useful for this.

      [ [None for x in xrange(10)] for y in xrange(10)]

      You could easily write a wrapper for it to clean the syntax a bit.

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

      #5; Wed, 07 May 2008 20:02:00 GMT
    • On Tue, 04 Jul 2006 22:07:59 -0700, bruce wrote:

      > do i need to import anything for this.. or is it supposed to work out of t

      he

      > box..

      Why don't you try it, and see if it gives you a result or raises an

      exception? The exception (if any) will give you a clue what you need to do.

      > and just what is it doing!!!!!

      The original list comprehension is:

      [[None for x in xrange(10)] for y in xrange(10)]

      This is a list comprehension inside another list comprehension. Start from

      the inside and work out. The inside list comp is:

      [None for x in xrange(10)]

      That is equivalent to:

      result = []

      for x in xrange(10):

      result.append(None)

      and it creates a list of ten None items.

      The outer list comp is

      [list_comp for y in xrange(10)]

      where list_comp is the inner list comprehension. It also creates a list

      of ten items, where each item is [None,None,...None]. But the important

      thing is that each of the inner lists are DIFFERENT lists that just happen

      to share the same value (ten None items), rather than the same list

      repeated ten times.

      Steven.

      #6; Wed, 07 May 2008 20:03:00 GMT
    • bruce <bedouglas.python.todaysummary.com.earthlink.net> wrote:

      > i need a multi dimensional array of lists...

      > ie

      > [q,a,d]

      > [q1,a1,d1]

      > [q2,a2,d2]

      > [q3,a3,d3]

      > which would be a (3,4) array...

      Multi-dimensional arrays aren't a built in feature of python.

      You can simulate them two ways

      1) with a list of lists

      ... [1,2,3],

      ... [4,5,6],

      ... [7,8,9],

      ... [10,11,12]

      ... ]

      5

      [[1, 2, 3], [4, 5, 6], [7, 'hello', 9], [10, 11, 12]]

      2) using a hash

      ... for y in range(3):

      ... a[x,y] = i

      ... i = i + 1

      ...

      5

      {(3, 2): 12, (3, 1): 11, (1, 2): 6, (1, 1): 5, (3, 0): 10, (0, 2): 3, (1, 0): 4, (0, 0): 1,

      (0, 1): 2, (2, 0): 7, (2, 1): 'hello', (2, 2): 9}

      Option 1) is the normal way of doing it in python. However

      initialising a multidimensional array always trips over beginners. Do

      it like this, where 0 is the initial value.

      [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

      Once you've done that you can write

      ... for y in range(3):

      ... a[x][y] = i

      ... i = i + 1

      ...

      5

      [[1, 2, 3], [4, 5, 6], [7, 'hello', 9], [10, 11, 12]]

      Numeric/scipy/numpy/whatever-it-is-called-today supports

      multidimensional arrays too I think and that may be more appropriate

      if you are doing heavy numerical work.

      Nick Craig-Wood <nick.python.todaysummary.com.craig-wood.com> -- http://www.craig-wood.com/nick

      #7; Wed, 07 May 2008 20:04:00 GMT
    • bruce wrote:

      > basic question..

      which means that it can usually be answered by reading the tutorial, the

      FAQ, or by googling for "python" plus your subject line...

      > how do i define a multi dimensional array

      > a[10][10]

      > is there a kind of a = array(10,10)

      http://pyfaq.infogami.com/how-do-i-...imensional-list

      </F>

      #8; Wed, 07 May 2008 20:05:00 GMT