我有一个多维数组。每次当我增加一个参数时,数组中就会创建更多的条目,数组会变得更大。resultsnresults随着每增加,我需要在数组上执行函数,这样每次增加都会有更多的参数添加到函数中。nreshape()resultsreshape()n例如,当 n=2 时:n = 2
arr_multi_dim = results.reshape(nrows, ncols, nrows, ncols)当 n=3 时:n = 3
arr_multi_dim = results.reshape(nrows, ncols, nrows, ncols, nrows, ncols)当 n=4 时:n = 4
arr_multi_dim = results.reshape(nrows, ncols, nrows, ncols, nrows, ncols, nrows, ncols)等。其中,在 的每个增量中,一组 和 被添加到函数中。nnrowsncolsreshape()如何编写函数,循环(或类似的东西),以便在我指定任何值时,将使用适当的函数?nreshape()提前非常感谢。
2 回答

慕的地6264312
TA贡献1817条经验 获得超6个赞
它看起来像参数解压缩的任务(别名星形运算符)。我会做的:
arr_multi_dim = results.reshape(*[nrows, ncols]*n)
[nrows, ncols]*2
变成、变成等等。[nrows, ncols, nrows, ncols]
[nrows, ncols]*3
[nrows, ncols, nrows, ncols, nrows, ncols]
添加回答
举报
0/150
提交
取消