Python Practice › NumPy Programs › Program 46
Sort each row
Write a NumPy program to sort each row in ascending order.
Input
[[6, 5], [10, 9], [8, 7]]
Output
[[ 5 6] [ 9 10] [ 7 8]]
Approach
np.sort(a, axis=1).
Solution
import numpy as npprint(np.sort(np.array([[6, 5], [10, 9], [8, 7]]), axis=1))Output
[[ 5 6] [ 9 10] [ 7 8]]
Learn the concept
New to this topic? Read the lesson, then come back.