多维列表(数组)重新分配问题
各位程序员和程序员们,大家好,
我正在编写一段代码,它会遍历一堆统计数据并返回我所要求的内容。 为了完成其任务,该方法从一个多维数组中读取数据并将其写入另一个多维数组中。 给我带来问题的代码是:
writer.variables[variable][:, :, :, :] = reader.variables[variable][offset:, 0, 0:5, 3]
两个切片的大小都是 27:1:6:1 但它抛出了一个异常:
ValueError: total size of new array must be unchanged
我大吃一惊。
谢谢。
Good day coders and codereses,
I am writing a piece of code that goes through a pile of statistical data and returns what I ask from it. To complete its task the method reads from one multidimensional array and writes into another one. The piece of code giving me problems is:
writer.variables[variable][:, :, :, :] = reader.variables[variable][offset:, 0, 0:5, 3]
The size of both slices is 27:1:6:1
but it throws up an exception:
ValueError: total size of new array must be unchanged
I am flabbergasted.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0:5
切片的大小不是您所说的 6:它是 5。切片中不包括上限(在 Python 中通常都是这样)。 不知道这是你的实际问题还是只是你问题中的拼写错误......The size of a slice with
0:5
is not 6 as you say: it's 5. The upper limit is excluded in slicing (as it most always is, in Python). Don't know whether that's your actual problem or just a typo in your question...