Python Array Slice With Comma?

前端 未结 3 1469
温柔的废话
温柔的废话 2020-11-27 05:25

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is

p =         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 05:38

    Empirically - create an array using numpy

    m = np.fromfunction(lambda i, j: (i +1)* 10 + j + 1, (9, 4), dtype=int)
    

    which assigns an array like below to m

    array(
          [[11, 12, 13, 14],
           [21, 22, 23, 24],
           [31, 32, 33, 34],
           [41, 42, 43, 44],
           [51, 52, 53, 54],
           [61, 62, 63, 64],
           [71, 72, 73, 74],
           [81, 82, 83, 84],
           [91, 92, 93, 94]])
    

    Now for the slice

    m[:,0]
    

    giving us

    array([11, 21, 31, 41, 51, 61, 71, 81, 91])
    

    I may have misinterpreted Khan Academy (so take with grain of salt):

    In linear algebra terms, m[:,n] is taking the nth column vector of the matrix m

    See Abhranil's note how this specific interpretation only applies to numpy

提交回复
热议问题