Multidimensional array in Python

前端 未结 12 1523
独厮守ぢ
独厮守ぢ 2021-02-04 15:49

I have a little Java problem I want to translate to Python. Therefor I need a multidimensional array. In Java it looks like:

double dArray[][][] = new double[x.l         


        
12条回答
  •  别那么骄傲
    2021-02-04 16:06

    Multidimensional arrays are a little murky. There are few reasons for using them and many reasons for thinking twice and using something else that more properly reflects what you're doing. [Hint. your question was thin on context ;-) ]

    If you're doing matrix math, then use numpy.

    However, some folks have worked with languages that force them to use multi-dimensional arrays because it's all they've got. If your as old as I am (I started programming in the 70's) then you may remember the days when multidimensional arrays were the only data structure you had. Or, your experience may have limited you to languages where you had to morph your problem into multi-dimensional arrays.

    Say you have a collection n 3D points. Each point has an x, y, z, and time value. Is this an n x 4 array? Or a 4 * n array? Not really.

    Since each point has 4 fixed values, this is more properly a list of tuples.

    a = [ ( x, y, z, t ), ( x, y, z, t ), ... ]
    

    Better still, we could represent this as a list of objects.

    class Point( object ):
        def __init__( self, x, y, z, t ):
            self.x, self.y, self.z, self.t = x, y, z, t
    
    a = [ Point(x,y,x,t), Point(x,y,z,t), ... ]
    

提交回复
热议问题