Python - numpy.lexsort() Function



In Python, the numpy.lexsort() function is used to perform an indirect sort using multiple keys. It sorts the elements based on the last key, followed by the second last, and so on. This is particularly useful for lexicographic (dictionary-like) ordering of multi-dimensional data.

The primary use of numpy.lexsort() function is to order data where sorting by multiple criteria is required, such as sorting records in a structured dataset. It returns an array of indices that can be used to obtain the sorted data.

Syntax

Following is the syntax of the Python numpy.lexsort() function −

numpy.lexsort(keys)

Parameters

Following are the parameters of the Python numpy.lexsort() function −

  • keys: A sequence of arrays, where each array represents a column of data. Sorting starts from the last key and progresses to the first.

Return Values

This function returns an array of indices that can be used to sort the data lexicographically.

Example

Following is a basic example of sorting data lexicographically using Python numpy.lexsort() function −

import numpy as np
# Define two keys
key1 = np.array([1, 2, 3, 1])
key2 = np.array([4, 3, 2, 1])
# Perform lexsort
indices = np.lexsort((key2, key1))
print("Sorted Indices:", indices)
# Use indices to sort data
sorted_key1 = key1[indices]
sorted_key2 = key2[indices]
print("Sorted Data:", list(zip(sorted_key1, sorted_key2)))

Output

Following is the output of the above code −

Sorted Indices: [3 0 1 2]
Sorted Data: [(1, 1), (1, 4), (2, 3), (3, 2)]

Example : Sorting Strings Lexicographically

The numpy.lexsort() function can also be used to sort strings by converting them to arrays. Below is an example where we sort names by last name followed by first name.

import numpy as np
# Define arrays for first and last names
first_names = np.array(['John', 'Alice', 'Bob'])
last_names = np.array(['Smith', 'Doe', 'Smith'])
# Perform lexsort
indices = np.lexsort((first_names, last_names))
print("Sorted Names:", [f"{last_names[i]}, {first_names[i]}" for i in indices])

Output

Following is the output of the above code −

Sorted Names: ['Doe, Alice', 'Smith, Bob', 'Smith, John']

Example : Multi-level Sorting

Using numpy.lexsort(), you can perform multi-level sorting. The keys are sorted from the last array to the first, allowing nested sorting.

In the following example, we sort an array of numbers based on two criteria: secondary and primary keys −

import numpy as np
# Define the keys
primary_key = np.array([1, 1, 2, 2])
secondary_key = np.array([4, 3, 2, 1])
# Perform lexsort
indices = np.lexsort((secondary_key, primary_key))
print("Sorted Indices:", indices)

Output

Following is the output of the above code −

Sorted Indices: [1 0 3 2]

Example : Sorting in Reverse Order

The numpy.lexsort() function can be combined with slicing to perform sorting in reverse order.

In the following example, we sort data in descending order by using reversed indices −

import numpy as np
key1 = np.array([1, 2, 3, 1])
key2 = np.array([4, 3, 2, 1])
# Perform lexsort
indices = np.lexsort((key2, key1))
# Reverse the indices 
reverse_indices = indices[::-1]
print("Reverse Sorted Indices:", reverse_indices)

Output

Following is the output of the above code −

Reverse Sorted Indices: [2 1 0 3]

Example : Working with Multi-dimensional Data

The numpy.lexsort() function can be used to sort multi-dimensional data when columns are treated as keys.

Here, we sort a 2D array row-wise based on two columns −

import numpy as np
data = np.array([[8, 7], [6, 5], [4, 3], [1, 2]])
# Transpose to use columns as keys
keys = data.T
# Perform lexsort
indices = np.lexsort(keys)
sorted_data = data[indices]
print("Sorted Data:\n", sorted_data)

Output

Following is the output of the above code −

Sorted Data:
 [[1 2]
 [4 3]
 [6 5]
 [8 7]]
Advertisements