banner



How To Get The Size Of An Array In Python

Get NumPy Assortment Length

  1. Get Length of a NumPy Array With the numpy.size Belongings in Python
  2. Become Length of a NumPy Array With the numpy.shape Function in Python

This tutorial volition discuss the methods to get the length of a NumPy array.

Get Length of a NumPy Array With the numpy.size Property in Python

The numpy.size belongings gets the full number of elements in a NumPy array. We can apply this property to accurately detect the number of elements in a NumPy array in Python. See the following code case.

              import numpy as np array = np.array([one,two,3,four,v,6,seven,8,nine]) print(array.size)                          

Output:

              nine                          

In the above lawmaking, we go the number of elements in the array assortment with the numpy.size belongings in Python. This method works bang-up with one-dimensional arrays. It does non consider the multi-dimensional arrays; it only gives us the total number of elements in an array. Information technology is shown in the lawmaking example below.

              import numpy equally np array = np.array([[i,ii,iii],[4,v,6],[7,8,ix]]) print(array.size)                          

Output:

              9                          

In the to a higher place code, we get the number of elements in the multi-dimensional array array with the numpy.size property in Python. It likewise gives us the value 9 because the full number of elements is the same as in the previous example. This is the reason why this method is not suitable for multi-dimensional arrays.

Get Length of a NumPy Array With the numpy.shape Function in Python

If we also want to know the number of elements in each dimension of the NumPy array, we have to employ the numpy.shape function in Python. The numpy.shape function returns a tuple in the class of (x, y), where x is the number of rows in the array and y is the number of columns in the array. We can notice the total number of elements in the array similar we have done in the previous department by multiplying both x and y with each other. Run across the following lawmaking example.

              import numpy equally np array = np.array([[one,2,3],[4,v,6],[7,eight,ix]]) print(array.shape)                          

Output:

              (three, three)                          

In the higher up code, we get the length of the multi-dimensional array array with the numpy.shape function in Python. We can now find the full number of elements by multiplying the values in the tuple with each other. This method is preferred over the previous method because information technology gives us the number of rows and columns.

Write for us

DelftStack articles are written by software geeks similar yous. If you likewise would like to contribute to DelftStack past writing paid articles, you can cheque the write for u.s.a. page.

Ezoic

How To Get The Size Of An Array In Python,

Source: https://www.delftstack.com/howto/numpy/numpy-array-length/

Posted by: dealcraver.blogspot.com

0 Response to "How To Get The Size Of An Array In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel