#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    Why no 2d array system in python?


    I recently started studying python and I am wondering if there is a way to have a 2d array.
    As far as I know there is only the concept of list in python.

    I am surprised that being a high level language why is it so difficult to do things like matrix multiplication etc(which require 2d arrays)?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    There are only a few programming languages that have nontrivial matrix operations as built-in functionality, and those that do (like Matlab and Mathematica) tend to be awkward for general-purpose (i.e., non-mathematical) programming. The preferred approach in general-purpose languages is to leave matrices and matrix arithmetic to third-party libraries.

    In Python, you have multiple options:
    - use a list of lists. You'll have to code your own matrix operations, and it won't be very efficient.

    - use NumPy, which I highly recommend. This gives you not only two-dimensional arrays, but arrays of arbitrary dimensionality. You also get all the standard matrix operations, including multiplication. The newest version of NumPy also includes the very useful einsum function, which is a generalization of matrix multiplication. (For example, this allows you to matrix-multiply one array of matrices elementwise against another array of matrices in one line of code.)

    - use another third-party library that implements some matrix functionality. I don't know of any that are as powerful as NumPy.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Numpy seems powerful. Thanks for telling

IMN logo majestic logo threadwatch logo seochat tools logo