元组作为多维数组的索引
问题描述
我发现了一个与我的问题非常相似的问题,但并不完全相同.这个:这里然而,在 ntimes 的情况下,数组的大小与元组所指向的维数相匹配.在我的例子中,我有一个 4 维数组和一个 2 维元组,就像这样:
I found a very similar question to mine, but not exactly the same. This one: here However in ntimes's case the size of the array matches the number of the dimensions the tuple is point at. In my case I have a 4-dimensional array and a 2-dimensional tuple, just like this:
我想使用元组作为前两个维度的索引,并手动索引后两个.比如:
I want to use the tuple as an index to the first two dimensions, and manually index the last two. Something like:
但是,我获得了索引 = 2 的第一个维度的重复,沿着第四个维度(因为它在技术上没有被索引).这是因为这个索引解释了对第一个维度的双索引,而不是每个维度的一个值,
However, I obtain a repetition of the first dimension with index=2, along the fourth dimension( since it technically hasn't been indexed). That is because this indexing is interpreting a double indexing to the first dimension instead of one value for each dimension,
那我怎样才能解包"这个元组呢?有任何想法吗?谢谢!
How can I 'unpack' this tuple then? Any ideas? thanks!
推荐答案
你也可以单独传入你的第一个元组来获取感兴趣的切片,然后单独索引它:
You can also pass in your first tuple alone to get the slice of interest, then index it seprately:
对于某些用户来说,这可能更具可读性,但否则我会倾向于 mgilson 的解决方案.
That may be slightly more readable for some users, but otherwise I'd lean towards mgilson's solution.
这篇关于元组作为多维数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!