Python Practice › NumPy Programs › Program 46

Sort each row

EasyNumPy Programs · 46 of 100

Write a NumPy program to sort each row in ascending order.

Advertisement
Input
[[6, 5], [10, 9], [8, 7]]
Output
[[ 5  6]
 [ 9 10]
 [ 7  8]]

Approach

  1. np.sort(a, axis=1).

Solution

Python
import numpy as npprint(np.sort(np.array([[6, 5], [10, 9], [8, 7]]), axis=1))
Output
[[ 5  6]
 [ 9 10]
 [ 7  8]]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs