Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
Would someone be able to provide bit by bit remarked examples with 1D and 2D arrays?

1 Answer

0 votes
by (26.4k points)

You can use np.where:

>>> a = np.arange(5,10)

>>> np.where(a < 8)       # tell me where in a, entries are < 8

(array([0, 1, 2]),)       # answer: entries indexed by 0, 1, 2

You can also use np.where if you want to get the entries in array which satisfies the condition

>>> a[np.where(a < 8)] 

array([5, 6, 7])          # selects from a entries 0, 1, 2

In case, if a is a 2d array, then np.where will returns the array of column idx's and arrary of row idx's:

>>> a = np.arange(4,10).reshape(2,3)

array([[4, 5, 6],

       [7, 8, 9]])

>>> np.where(a > 8)

(array(1), array(2))

In case of 1d array, np.where will be used to get the entries inm 2d array which will satisfies the condition:

>>> a[np.where(a > 8)] # selects from a entries 0, 1, 2

array([9])

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

Related questions

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...