how to get the neighboring elements in a numpy array with taking boundaries into account?

前端 未结 4 848
不知归路
不知归路 2021-01-01 02:40

I want to get the neighbors of the certain element in the numpy array. Lets consider following example

    a = numpy.array([0,1,2,3,4,5,6,7,8,9])
         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-01 03:17

    Python takes care of boundaries for you:

    >>> a = [0,1,2,3,4,5,6,7,8,9]
    >>> a[-100 : 1000]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> a[-100:3]
    [0, 1, 2]
    

提交回复
热议问题