[Python] Accessing Array Elements

NumPy Reference: Indexing

Note: Indexing starts at 0 (zero).

 

def slicing():
    a = np.random.rand(5,4)
    print(a)
    """
    [[ 0.15372787  0.83347785  0.86855635  0.23592008]
    [ 0.74925447  0.08453883  0.62021973  0.51972556]
    [ 0.33835541  0.03772665  0.53220179  0.2095391 ]
    [ 0.41991738  0.76028856  0.22081936  0.4240198 ]
    [ 0.93666236  0.72688287  0.99309427  0.69388106]]
    """
    print(a[0:2, 0:4:3]) #0:4:3 X:Y:Z, if Y > Z, z is step, so here is take columns from [0,4), step is 3
    """
   [[ 0.15372787 0.23592008] [ 0.74925447 0.51972556]]
"""
if __name__ == "__main__": slicing()

 

posted @ 2017-12-18 21:21  Zhentiw  阅读(212)  评论(0编辑  收藏  举报