在python中,最好是作为numpy数组,我怎样才能在php中获得数据结构,就像这样:
$mdmat = array();
for($i=0;$i<50;$i++)
for($x=90;$x<=510;$x+=30)
for($y=50;$y<470;$y+=30)
$mdmat[$i][$x][$y] = rand(0,1000);
所以我以后可以像这样改变它的元素的值:
$mdmat[1][120][80]= 5;
我需要那些数组索引。
这是该结构的转储:https : //gist.github.com/acosonic/68333d286b5684e42fbdf5b28bcf9101
您正在跳过内部循环中的索引。如果你想保留它,那么这不是一个真正的数组,而是一个键值对映射,也就是字典。
字典中不能有可变键,但可以使用元组。
import random
mdmat = {}
for i in range(0, 50):
for x in range(90, 510 + 1, 30): #the +1 is to handle <= condition, range are right exclusive
for y in range(50, 470, 30):
mdmat[(i, x, y)] = random.randint(0,1000)
编辑:查看您的编辑,您可以通过一个小的调整来实现与嵌套字典相同的概念。
import random
mdmat_root = {}
for i in range(0, 50):
mdmat_root[i] = {}
for x in range(90, 510 + 1, 30): #the +1 is to handle <= condition, range are right exclusive
mdmat_root[i][x] = {}
for y in range(50, 470, 30):
mdmat_root[i][x][y] = random.randint(0,1000)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句