Define a 2d Array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jillian Calderon

    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
  • skip@pobox.com

    #2
    Re: Define a 2d Array?


    JillHow 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 multidimensiona l lists you
    should consider the builtin array object or the numpy package:

    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, e...



    Skip

    Comment

    • Eric_Dexter@msn.com

      #3
      Re: Define a 2d Array?

      On Oct 11, 9:30 pm, s...@pobox.com wrote:
          JillHow 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 multidimensiona l lists you
      should consider the builtin array object or the numpy package:
      >
         http://docs.python.org/dev/library/a...l#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.

      Comment

      • John Machin

        #4
        Re: Define a 2d Array?

        On Oct 12, 1:30 pm, s...@pobox.com wrote:
            JillHow do I define a 2d list?
        >
        If you are looking to do numeric work with such multidimensiona l lists you
        should consider the builtin array object or the numpy package:
        >
           http://docs.python.org/dev/library/a...l#module-array
        The built-in array module does *NOT* support multidimensiona l 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

        Comment

        • skip@pobox.com

          #5
          Re: Define a 2d Array?


          EricI think you can do

          Ericmylist = [[]] 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.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0], [0.0, 42.700000000000 003, 0.0, 0.0, 0.0]]

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

          Skip

          Comment

          • thomas.p.krauss@gmail.com

            #6
            Re: Define a 2d Array?

            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

            Comment

            • Jillian Calderon

              #7
              Re: Define a 2d Array?

              Thanks, everyone. All of your help solved at least my data storage
              problem!

              Comment

              Working...