arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul Garg

    #1

    arrays

    Hi.
    Is there someway i can get something similar to multi-dimensional
    arrays in python.I dont want to use Numarray.
    rahul
  • John Hunter

    #2
    Re: arrays

    >>>>> "Rahul" == Rahul Garg <codedivine@gma il.com> writes:

    Rahul> Hi. Is there someway i can get something similar to
    Rahul> multi-dimensional arrays in python.I dont want to use
    Rahul> Numarray

    Yes, you can use lists of lists for example.

    x = [ [None for i in range(10)] for j in range (12)]
    x[1][2] = 1

    But it would help if you stated what your requirements are for the
    multi-dim array, what kind of objects you want to store in them, what
    kind of operations you need to be able to do, what your performance
    requirements are, and why you don't want to use numarray or Numeric
    for that matter.


    JDH

    Comment

    • Diez B. Roggisch

      #3
      Re: arrays

      Rahul Garg wrote:
      [color=blue]
      > Hi.
      > Is there someway i can get something similar to multi-dimensional
      > arrays in python.I dont want to use Numarray.
      > rahul[/color]

      Use lists in lists.
      [color=blue][color=green][color=darkred]
      >>> foo = [[1,2], [3,4]]
      >>> print foo[0][1][/color][/color][/color]
      2


      --
      Regards,

      Diez B. Roggisch

      Comment

      • Scott David Daniels

        #4
        Re: arrays

        Robert Kern wrote:[color=blue]
        > Rahul Garg wrote:[color=green]
        >> 1. I will mostly be storing floating point numbers in 2 dimensional
        >> arrays which i will pass to a custom module written in C. The
        >> application is for scientific computing purposes.I just need python +
        >> wxPython for the GUI.
        >>
        >> 2.I am not using Numarray because i dont expect to do many operations
        >> on the matrices in Python itself. Most of that stuff will be handled
        >> in my C module.[/color]
        >
        > You still might want to use numarray or Numeric in this case. The memory
        > representation of a numarray/Numeric array is the same as in C. You
        > won't have to duplicate memory and waste time converting between a
        > memory block of doubles and lists of lists of Python floats.[/color]

        If you want to refer to a block of memory that you share with your C
        code (especially if the C code controls the memory, you might also want
        to check out my "Blocks and Views" code at:



        -Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        • Carl Banks

          #5
          Re: arrays

          codedivine@gmai l.com (Rahul Garg) wrote in message news:<a2f50cd.0 411151042.45487 164@posting.goo gle.com>...[color=blue]
          > Hi.
          > Is there someway i can get something similar to multi-dimensional
          > arrays in python.I dont want to use Numarray.
          > rahul[/color]

          It seems (from other posts) as if you're looking for a way to get
          simple multidimensiona l array behavior in Python, while making
          convenient to access it in C, without using numarray or Numeric.

          Probably the simplest way to accomplish this without numarray is to
          use a class to wrap a list. Override __getitem__ and __setitem__ to
          get the effect of multidimensiona l access. Because the data is stored
          in a single list, rather than a list of lists, it is convenient to
          access it from C. A minimal example follows.

          class MinimalMultiArr ay:
          def __init__(self,r ows,cols):
          self.rows = rows
          self.cols = cols
          self.data = [0.0] * self.rows * self.cols
          def __getitem__(sel f,(row,column)) :
          return self.data[row*self.cols + column]
          def __setitem__(sel f,(row,column), value):
          self.data[row*self.cols + column] = value

          Notice that Python can unwrap tuples right into function arguments, so
          if you reference array[1,2], in __getitem__, row will be set to one
          and column to two.

          Any improvements left as an exercise. Here's a suggestion: check
          whether row and column arguments are slice objects, and return a list
          or a smaller multidimensiona l array if so.


          --
          CARL BANKS

          Comment

          • Scott David Daniels

            #6
            Re: arrays

            Scott David Daniels wrote:[color=blue]
            > If you want to refer to a block of memory that you share with your C
            > code (especially if the C code controls the memory, you might also want
            > to check out my "Blocks and Views" code at:
            >
            > http://members.dsl-only.net/~daniels/block.html[/color]

            To follow up on my own post, once you have a View v of, say, 100
            floating point data entries:
            [color=blue][color=green][color=darkred]
            >>> multidim = [v[x : x+1] for x in range(0, 100, 10)]
            >>> revdim = [v[x : 100 : 10] for x in range(10)]
            >>> v[27][/color][/color][/color]
            43.6[color=blue][color=green][color=darkred]
            >>> multidim[2][7][/color][/color][/color]
            43.6[color=blue][color=green][color=darkred]
            >>> revdim[7][2][/color][/color][/color]
            43.6[color=blue][color=green][color=darkred]
            >>> multidim[2][7] = 19.1
            >>> multidim[2][7][/color][/color][/color]
            19.1[color=blue][color=green][color=darkred]
            >>> revdim[7][2][/color][/color][/color]
            19.1[color=blue][color=green][color=darkred]
            >>> v[27][/color][/color][/color]
            19.1

            That is, you maintain the mapping to the original memory when taking
            strides. All subscript operations change how the memory is referenced,
            not take copies of the memory.

            --Scott David Daniels
            Scott.Daniels@A cm.Org

            Comment

            • Isaac To

              #7
              Re: arrays

              >>>>> "Rahul" == Rahul Garg <codedivine@gma il.com> writes:

              Rahul> Hi. Is there someway i can get something similar to
              Rahul> multi-dimensional arrays in python.I dont want to use
              Rahul> Numarray.

              Is an array of array sufficient? Thinks like [[0]*5]*3.

              Regards,
              Isaac.

              Comment

              • Duncan Booth

                #8
                Re: arrays

                Isaac To wrote:
                [color=blue][color=green][color=darkred]
                >>>>>> "Rahul" == Rahul Garg <codedivine@gma il.com> writes:[/color][/color]
                >
                > Rahul> Hi. Is there someway i can get something similar to
                > Rahul> multi-dimensional arrays in python.I dont want to use
                > Rahul> Numarray.
                >
                > Is an array of array sufficient? Thinks like [[0]*5]*3.
                >[/color]
                You were right to call that an array of array (singular): what you
                suggested creates a list containing 3 references to the same list. I
                suspect the OP is much more likely to want an array of arrays (plural).

                The cleanest way these days is to use a list comprehension for all but the
                innermost list (provided the initial value is immutable):

                [[0]*5 for i in range(3)]

                or, if you want something mutable or more complex for an initial value you
                can just use list comprehensions throughout e.g.:

                [ [(i*100+j) for i in range(5)] for j in range(3) ]

                Comment

                Working...