Multidimensional array in Python

前端 未结 12 1520
独厮守ぢ
独厮守ぢ 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:29

    I've just stepped into a similar need and coded this:

    def nDimensionsMatrix(dims, elem_count, ptr=[]):
        if (dims > 1):
            for i in range(elem_count[dims-1]):
                empty = []
                ptr.append(empty)
                nDimensionsMatrix(dims-1, elem_count, empty)
            return ptr
        elif dims == 1:
            ptr.extend([0 for i in range(elem_count[dims])])
            return ptr
    
    matrix = nDimensionsMatrix(3, (2,2,2))
    

    I'm not looking at speed, only funcionality ;)

    I want to create a matrix with N dimensions and initialize with 0 (a elem_count number of elements in each dimension).

    Hope its helps someone

提交回复
热议问题