为了账号安全,请及时绑定邮箱和手机立即绑定

如何以迭代方式将参数添加到函数中

如何以迭代方式将参数添加到函数中

哔哔one 2022-08-25 15:10:39
我有一个多维数组。每次当我增加一个参数时,数组中就会创建更多的条目,数组会变得更大。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]


查看完整回答
反对 回复 2022-08-25
?
撒科打诨

TA贡献1934条经验 获得超2个赞

也许像这样?

results.reshape(*[nrows,ncol]*n)


查看完整回答
反对 回复 2022-08-25
  • 2 回答
  • 0 关注
  • 75 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号