- 这里指的是维度为A*B*C*?的规整的高维数组。
- 方法1:直接嫖numpy提供的接口
import numpy as np a = np.zeros((3,3,0)).tolist() # 这样的话创建出的其实是3*3*?的数组,最后一维是动态的 # 如果希望最后一维是固定长度的话,把参数中的0替换成想要的长度也就可以了,这样的话是用0填充了
- 方法2:手撸函数
def high_dim_list(shape, fix_last = False, fix = 0): if len(shape) == 0: if fix_last: return fix else: tmp = [] return tmp else: tmp = [] for i in range(shape[0]): tmp.append(high_dim_list(shape[1:], fix_last = fix_last, fix = fix)) return tmp a = high_dim_list([3,3]) # 效果和上面是一样的 # 如果需要固定最后一维长度,且填充一些东西用下面这句 a = high_dim_list([3,3], fix_last = True, fix = 0)
- 如果发现有误可以评论告知下
python 创建高维数组列表
最新推荐文章于 2024-07-06 13:52:14 发布