w3resource logo
Python Exercises

Python NumPy: Sort pairs of first name and last name return their indices

Python NumPy: Array Object Exercise-30 with Solution

Write a Python program to sort pairs of first name and last name return their indices. (first by last name, then by first name).
first_names = (‘Betsey’, ‘Shelley’, ‘Lanell’, ‘Genesis’, ‘Margery’)
last_names = (‘Battle’, ‘Brien’, ‘Plotner’, ‘Stahl’, ‘Woolum’)

Sample Solution:-

Python Code:

import numpy as np
first_names =    ('Margery', 'Betsey', 'Shelley', 'Lanell', 'Genesis')
last_names = ('Woolum', 'Battle', 'Plotner', 'Brien', 'Stahl')
x = np.lexsort((first_names, last_names))
print(x)

Sample Output:

[1 3 2 4 0] 

Python Code Editor:

import numpy as np
first_names =    ('Margery', 'Betsey', 'Shelley', 'Lanell', 'Genesis')
last_names = ('Woolum', 'Battle', 'Plotner', 'Brien', 'Stahl')
x = np.lexsort((first_names, last_names))
print(x)

Improve this sample solution and post your code through Disqus