﻿ NumPy: Create an array that represents the rank of each item of a given array - w3resource # NumPy: Create an array that represents the rank of each item of a given array

## NumPy: Array Object Exercise-147 with Solution

Write a NumPy program to create an array that represents the rank of each item of a given array.

Sample Solution:

Python Code:

``````import numpy as numpy
array = numpy.array([24, 27, 30, 29, 18, 14])
print("Original array:")
print(array)
argsort_array = array.argsort()
ranks_array = numpy.empty_like(argsort_array)
ranks_array[argsort_array] = numpy.arange(len(array))
print("\nRank of each item of the said array:")
print(ranks_array)
```
```

Sample Output:

```Original array:
[24 27 30 29 18 14]

Rank of each item of the said array:
[2 3 5 4 1 0]
```

Pictorial Presentation: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Set comprehension:

```>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}
```