12 Sorting Searching And Counting Solutions


title: "Soring, searching, and counting" date: 2026-05-24T13:51:13Z

Soring, searching, and counting

1import numpy as np
1np.__version__
'1.11.2'
1author = 'kyubyong. longinglove@nate.com'

Sorting

Q1. Sort x along the second axis.

1x = np.array([[1,4],[3,1]])
2out = np.sort(x, axis=1)
3x.sort(axis=1)
4assert np.array_equal(out, x)
5print out
[[1 4]
 [1 3]]

Q2. Sort pairs of surnames and first names and return their indices. (first by surname, then by name).

1surnames =    ('Hertz',    'Galilei', 'Hertz')
2first_names = ('Heinrich', 'Galileo', 'Gustav')
3print np.lexsort((first_names, surnames))
[1 2 0]

Q3. Get the indices that would sort x along the second axis.

1x = np.array([[1,4],[3,1]])
2out = np.argsort(x, axis=1)
3print out
[[0 1]
 [1 0]]

Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value.

1x = np.random.permutation(10)
2print "x =", x
3print "\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n", 
4out = np.partition(x, 5)
5x.partition(5) # in-place equivalent
6assert np.array_equal(x, out)
7print out
x = [5 1 6 3 9 8 2 7 4 0]

Check the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5
[2 0 4 3 1 5 8 7 6 9]

Q5. Create the indices of an array such that its third element would be the same as the element of sorted x, and it divide other elements by their value.

1x = np.random.permutation(10)
2print "x =", x
3partitioned = np.partition(x, 3)
4indices = np.argpartition(x, 3)
5print "partitioned =", partitioned
6print "indices =", partitioned
7assert np.array_equiv(x[indices], partitioned)
x = [2 8 3 7 5 6 4 0 9 1]
partitioned = [0 1 2 3 4 5 8 6 9 7]
indices = [0 1 2 3 4 5 8 6 9 7]

Searching

Q6. Get the maximum and minimum values and their indices of x along the second axis.

1x = np.random.permutation(10).reshape(2, 5)
2print "x =", x
3print "maximum values =", np.max(x, 1)
4print "max indices =", np.argmax(x, 1)
5print "minimum values =", np.min(x, 1)
6print "min indices =", np.argmin(x, 1)
x = [[0 5 9 8 2]
 [3 7 4 1 6]]
maximum values = [9 7]
max indices = [2 1]
minimum values = [0 1]
min indices = [0 3]

Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs.

1x = np.array([[np.nan, 4], [3, 2]])
2print "maximum values ignoring NaNs =", np.nanmax(x, 1)
3print "max indices =", np.nanargmax(x, 1)
4print "minimum values ignoring NaNs =", np.nanmin(x, 1)
5print "min indices =", np.nanargmin(x, 1)
maximum values ignoring NaNs = [ 4.  3.]
max indices = [1 0]
minimum values ignoring NaNs = [ 4.  2.]
min indices = [1 1]

Q8. Get the values and indices of the elements that are bigger than 2 in x.

1x = np.array([[1, 2, 3], [1, 3, 5]])
2print "Values bigger than 2 =", x[x>2]
3print "Their indices are ", np.nonzero(x > 2)
4assert np.array_equiv(x[x>2], x[np.nonzero(x > 2)])
5assert np.array_equiv(x[x>2], np.extract(x > 2, x))
Values bigger than 2 = [3 3 5]
Their indices are  (array([0, 1, 1], dtype=int64), array([2, 1, 2], dtype=int64))

Q9. Get the indices of the elements that are bigger than 2 in the flattend x.

1x = np.array([[1, 2, 3], [1, 3, 5]])
2print np.flatnonzero(x>2)
3assert np.array_equiv(np.flatnonzero(x), x.ravel().nonzero())
[2 4 5]

Q10. Check the elements of x and return 0 if it is less than 0, otherwise the element itself.

1x = np.arange(-5, 4).reshape(3, 3)
2print np.where(x <0, 0, x)
[[0 0 0]
 [0 0 0]
 [1 2 3]]

Q11. Get the indices where elements of y should be inserted to x to maintain order.

1x = [1, 3, 5, 7, 9]
2y = [0, 4, 2, 6]
3np.searchsorted(x, y)
array([0, 2, 1, 3], dtype=int64)

Counting

Q12. Get the number of nonzero elements in x.

1x = [[0,1,7,0,0],[3,0,0,2,19]]
2print np.count_nonzero(x)
3assert np.count_nonzero(x) == len(x[x!=0])
5