Return equivalent of `:` from function for indexing array

前端 未结 3 774
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 05:03

I have a large array and a function that returns index lists into the array, i.e.,

import numpy

n = 500
a = numpy.random.rand(n)

def get_idx(k):
    # M         


        
相关标签:
3条回答
  • 2020-12-21 05:17

    Have a look at slice

    def get_x():
        return slice(2)
    
    a=list(range(100))
    a[get_x()]
    

    will return [0, 1]

    UPDATE

    And for your need get_x function should be

    def get_x(k, n):
        return slice(n if k > 6 else k)
    

    Update

    as @Eric correctly noted it's better to pass None instead of n. So function would be:

    def get_x(k):
        return slice(None if k > 6 else k)
    
    0 讨论(0)
  • 2020-12-21 05:28

    NumPy has a helper np.s_[] which can be used to construct slice and Ellipsis objects:

    def get_idx(k):
        return np.s_[:] if k > 6 else np.s_[:k]
    
        # or even np.s_[:None if k > 6 else k]
    

    In general, a[np.s_[ <stuff> ]] is exactly the same as a[ <stuff> ].

    0 讨论(0)
  • 2020-12-21 05:34

    You can use Ellipsis (translates to '...')

    np.identity(2)[Ellipsis]
    # array([[1.0, 0.0], [0.0, 1.0]])
    

    Edit:

    I feel I should mention one caveat: If you are sure this will only be used on 1d arrays, it's a perfectly fine and simple solution. However, in a public interface I wouldn't recommend it because it doesn't generalise correctly if a user uses the output of getidx for indexing a multi dimensional array.

    For example A[getidx(k), ...] won't be safe anymore and the semantics of A[getidx(k), someslice] will be inconsistent if A has three or more dims.

    That said, slice(None) isn't perfect in this respect either, for exammple A[getidx(k), getidx(k)] will behave inconsistently.

    arange(n) looks like it's always giving the correct behaviour, but of course it's wasteful in most cases.

    0 讨论(0)
提交回复
热议问题