创建数组数组(8x8,每个数组为3x3)的Python代码

2024-04-25 20:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个数组数组,其结构为8x8,其中每个单元格都是3x3数组。我所创造的是可行的,但是当我想要改变一个特定的值时,我需要以不同于我预期的方式访问它。你知道吗

import numpy as np
a = np.zeros((3,3))
b = np.array([[0,1,0],[1,1,1],[0,1,0]])
d = np.array([[b,a,b,a,b,a,b,a]])
e = np.array([[a,b,a,b,a,b,a,b]])
g = np.array([[d],[e],[d],[e],[d],[e],[d],[e]])

#Needed to change a specific cell 
#g[0][0][0][0][0][0] = x : [Row-x][0][0][Cell-x][row-x][cell-x]
#Not sure why I have to have the 2 0's between the Row-x and the Cell-x identifiers

在此之后,我需要将每个值映射到一个24x24的网格,其中1的颜色与0的颜色不同。如果有人能提供实现这一点的方向,将不胜感激。不是寻找具体的代码,而是了解如何实现的基础。你知道吗

谢谢


Tags: thetoimportnumpy颜色haveasnp
1条回答
网友
1楼 · 发布于 2024-04-25 20:19:18
In [291]: a = np.zeros((3,3)) 
     ...: b = np.array([[0,1,0],[1,1,1],[0,1,0]]) 
     ...: d = np.array([[b,a,b,a,b,a,b,a]]) 
     ...: e = np.array([[a,b,a,b,a,b,a,b]]) 
     ...: g = np.array([[d],[e],[d],[e],[d],[e],[d],[e]])                       

In [292]: a.shape                                                               
Out[292]: (3, 3)
In [293]: b.shape                                                               
Out[293]: (3, 3)

d是4d-计算括号:[[....]]

In [294]: d.shape                                                               
Out[294]: (1, 8, 3, 3)
In [295]: e.shape                                                               
Out[295]: (1, 8, 3, 3)

g是4个小元素的(8,1),总共6个。再次计算括号:

In [296]: g.shape                                                               
Out[296]: (8, 1, 1, 8, 3, 3)

访问2d子阵列,在本例中等于b

In [298]: g[0,0,0,0,:,:]                                                        
Out[298]: 
array([[0., 1., 0.],
       [1., 1., 1.],
       [0., 1., 0.]])

重做,没有多余的括号:

In [299]: a = np.zeros((3,3)) 
     ...: b = np.array([[0,1,0],[1,1,1],[0,1,0]]) 
     ...: d = np.array([b,a,b,a,b,a,b,a]) 
     ...: e = np.array([a,b,a,b,a,b,a,b]) 
     ...: g = np.array([d,e,d,e,d,e,d,e])                                       
In [300]: d.shape                                                               
Out[300]: (8, 3, 3)
In [301]: g.shape                                                               
Out[301]: (8, 8, 3, 3)

相关问题 更多 >

    热门问题