Index a numpy array with another array

前端 未结 2 1725
春和景丽
春和景丽 2021-01-18 02:19

I feel silly, because this is such a simple thing, but I haven\'t found the answer either here or anywhere else.

Is there no straightforward way of indexing a numpy

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 02:55

    Just use a tuple:

    >>> A[(3, 1)]
    8
    >>> A[tuple(ind)]
    8
    

    The A[] actually calls the special method __getitem__:

    >>> A.__getitem__((3, 1))
    8
    

    and using a comma creates a tuple:

    >>> 3, 1
    (3, 1)
    

    Putting these two basic Python principles together solves your problem.

    You can store your index in a tuple in the first place, if you don't need NumPy array features for it.

提交回复
热议问题