Create 3D array using Python

前端 未结 9 1193
情话喂你
情话喂你 2020-12-23 09:57

I would like to create a 3D array in Python (2.7) to use like this:

distance[i][j][k]

And the sizes of the array should be the size of a vari

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 10:29

    The right way would be

    [[[0 for _ in range(n)] for _ in range(n)] for _ in range(n)]
    

    (What you're trying to do should be written like (for NxNxN)

    [[[0]*n]*n]*n
    

    but that is not correct, see @Adaman comment why).

提交回复
热议问题