[英]Use array to define indices for multidimensional numpy array
I have a multidimensional Numpy array;我有一个多维 Numpy 数组; let's say it's假设它是
myArray = array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
I know that running myArray[1,1,1]
, for instance, will return 13. However, I want to define indx = [1,1,1]
then call something to the effect of myArray[indx]
.例如,我知道运行myArray[1,1,1]
将返回 13。但是,我想定义indx = [1,1,1]
然后调用一些东西来达到myArray[indx]
的效果。 However, this does some other multidimensional indexing stuff.但是,这会做一些其他的多维索引工作。
I have also tried myArray[*indx]
but that understandably throws a syntax error.我也尝试过myArray[*indx]
但可以理解的是会引发语法错误。
Currently my very ugly workaround is to define目前我非常丑陋的解决方法是定义
def array_as_indices(array, matrix):
st = ''
for i in array:
st += '%s,' % i
st = st[:-1]
return matrix[eval(st)]
which works but is quite inelegant and presumably slow.它有效但非常不优雅并且可能很慢。
Is there a more pythonic way to do what I'm looking for?有没有更 pythonic 的方式来做我正在寻找的事情?
This is a duplicate of Unpacking tuples/arrays/lists as indices for Numpy Arrays , but you can just create a tuple这是Unpacking tuples/arrays/lists as indices for Numpy Arrays 的副本,但您可以只创建一个元组
import numpy as np
def main():
my_array = np.array(
[
[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
[[9, 10, 11], [12, 13, 14], [15, 16, 17]],
[[18, 19, 20], [21, 22, 23], [24, 25, 26]],
]
)
print(f"my_array[1,1,1]: {my_array[1,1,1]}")
indx = (1, 1, 1)
print(f"my_array[indx]: {my_array[indx]}")
if __name__ == "__main__":
main()
will give会给
my_array[1,1,1]: 13
my_array[indx]: 13
The indices of a numpy array are addressed by tuples, not lists. numpy 数组的索引由元组而不是列表寻址。 Use indx = (1, 1, 1)
.使用indx = (1, 1, 1)
。
As an extension, if you want to call the indices (1, 1, 1) and (2, 2, 2), you can use作为扩展,如果你想调用索引 (1, 1, 1) 和 (2, 2, 2),你可以使用
>>> indx = ([1, 2], [1, 2], [1, 2])
>>> x[indx]
array([13, 26])
The rationale behind the behavior with lists is that numpy treats lists sequentially, so列表行为背后的基本原理是 numpy 按顺序处理列表,所以
>>> indx = [1, 1, 1]
>>> x[indx]
array([[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]])
It returns a list of three elements, each equal to x[1].它返回一个包含三个元素的列表,每个元素都等于 x[1]。
I have a multidimensional Numpy array;我有一个多维 Numpy 数组; let's say it's假设是
myArray = array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
I know that running myArray[1,1,1]
, for instance, will return 13. However, I want to define indx = [1,1,1]
then call something to the effect of myArray[indx]
.例如,我知道运行myArray[1,1,1]
将返回 13。但是,我想定义indx = [1,1,1]
然后调用myArray[indx]
的效果。 However, this does some other multidimensional indexing stuff.但是,这会做一些其他多维索引的事情。
I have also tried myArray[*indx]
but that understandably throws a syntax error.我也试过myArray[*indx]
但这会引发语法错误,这是可以理解的。
Currently my very ugly workaround is to define目前我非常丑陋的解决方法是定义
def array_as_indices(array, matrix):
st = ''
for i in array:
st += '%s,' % i
st = st[:-1]
return matrix[eval(st)]
which works but is quite inelegant and presumably slow.这有效,但很不优雅,而且可能很慢。
Is there a more pythonic way to do what I'm looking for?有没有更蟒蛇的方式来做我正在寻找的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.