我在python中声明多维数组

Nbrs[23][2] = [[1, 1], [1, 2], [2, 1],
               [2, 3], [3, 2], [1, 3],
               [3, 1], [1, 4], [3, 4],
               [4, 3], [4, 1], [1, 5],
               [2, 5], [3, 5], [4, 5],
               [5, 4], [5, 3], [5, 2],
               [5, 1], [1, 6], [5, 6],
               [6, 5], [6, 1]
           ]


它给我的错误是:

NameError: name 'Nbrs' is not defined


我不能以此方式在python中声明二维数组吗?

最佳答案

转让声明:

Nbrs[23][2] = [[1, 1], [1, 2], [2
#    ^  ^ you can't index   Nbrs before it created


应该:

Nbrs = [[1, 1], [1, 2], [2
# now after this statement, Nbrs a list of list you can access
# its elements useng `Nbrs[i][j]`  for i < len(Nbrs) and j < 2


我认为您由于C,C ++声明而感到困惑!

关于python - 在python中声明和初始化2d数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21201286/

10-12 21:38