Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: how to declare a 2D array in python

  1. #1
    Join Date
    Jan 2006
    Location
    Florida
    Beans
    377
    Distro
    Ubuntu 6.06 Dapper

    how to declare a 2D array in python

    I am trying to execute the following code

    Code:
    a =[][]
    for i in range(3):
        for j in range(3):
            a[i][j] = i+j
    
    print a
    I get an error "invalid syntax". How to fix this bug.

    Thanks for the help

  2. #2
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: how to declare a 2D array in python

    You need to append values into the lists

    Code:
    >>> a = []
    >>> for i in xrange(3):
    ...     a.append([])
    ...     for j in xrange(3):
    ...             a[i].append(i+j)
    ...
    >>> a
    [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
    >>>
    ACCU - for programmers who care

  3. #3
    Join Date
    Jan 2006
    Location
    Florida
    Beans
    377
    Distro
    Ubuntu 6.06 Dapper

    Re: how to declare a 2D array in python

    Thanks, it worked like a charm.

    Quote Originally Posted by thumper
    You need to append values into the lists

    Code:
    >>> a = []
    >>> for i in xrange(3):
    ...     a.append([])
    ...     for j in xrange(3):
    ...             a[i].append(i+j)
    ...
    >>> a
    [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
    >>>

  4. #4
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: how to declare a 2D array in python

    Just to put, there is an easier way, have a look:

    To create an n by n 2D array:
    Code:
    a = [[]*n for x in xrange(n)]
    In case you want to initialize each element with some value v
    Code:
    a = [[v]*n for x in xrange(n)]
    Test it
    Code:
    print a
    Last edited by zobayer1; December 10th, 2010 at 10:51 PM.

  5. #5
    Join Date
    May 2008
    Location
    UK
    Beans
    1,450
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: how to declare a 2D array in python

    if it is going to be a sparsley populated array, that you need to index with two integers, you might find that a dictionary with a tuple (x,y) as the key might work just as well.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  6. #6
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: how to declare a 2D array in python

    and if you are dealing with arrays of numbers then have a look at numpy.

  7. #7
    Join Date
    Sep 2011
    Beans
    3

    Re: how to declare a 2D array in python

    You should also be able to type this in:
    Code:
    l_variable = [[[2]*3]*4]*5
    This will create a three dimensional array with the following properties

    • all values will be populated with '2'
    • the first dimension will be length 3
    • the second dimension will be length 4
    • the third dimension will be length 5

    Because Python does not require type declaration when creating a new variable, I choose to put a prefix in front of my variables. In this instance I use the "l_" in front of my list variables.

    This has been tested with Python 2.7.1
    Last edited by Pipeliner; September 16th, 2011 at 07:48 PM. Reason: Include which Python release this has been tested with

  8. #8
    Join Date
    Jul 2006
    Beans
    193

    Re: how to declare a 2D array in python

    Quote Originally Posted by Pipeliner View Post
    You should also be able to type this in:
    Code:
    l_variable = [[[2]*3]*4]*5
    This will create a three dimensional array
    Yes, it will, but a three dimensional array of what?

    Independent lists, or references to a single list?

    This would be a good test to run:

    PHP Code:
    l_variable[0][0][0] = 5
    print l_variable 
    Be careful.

  9. #9
    Join Date
    Sep 2011
    Beans
    3

    Re: how to declare a 2D array in python

    This will be the result of:
    Code:
    l_variable = [[[2]*3]*4]*5
    [[[2,2,2], [2,2,2], [2,2,2], [2,2,2]], [[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]]]

  10. #10
    Join Date
    Jul 2006
    Beans
    193

    Re: how to declare a 2D array in python

    Run this:

    PHP Code:
    l_variable = [[[2]*3]*4]*5
    print l_variable

    l_variable
    [0][0][0] = 5
    print l_variable 
    See the problem now?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •