Find length of 2D array Python

前端 未结 6 1635
一生所求
一生所求 2020-12-07 15:48

How do I find how many rows and columns are in a 2d array?

For example,

Input = ([[1, 2], [3, 4], [5, 6]])`

should be displaye

6条回答
  •  感动是毒
    2020-12-07 15:59

    You can use numpy.shape.

    import numpy as np
    x = np.array([[1, 2],[3, 4],[5, 6]])
    

    Result:

    >>> x
    array([[1, 2],
           [3, 4],
           [5, 6]])
    >>> np.shape(x)
    (3, 2)
    

    First value in the tuple is number rows = 3; second value in the tuple is number of columns = 2.

提交回复
热议问题