nmaxmin module in Python

We use the nmaxmin module in Python to find nth minimum or maximum of a number in a given list. It is a simple package that needs to be installed first in our system. Let’s see how we can install it and use it.

nmaxmin module: Installation and usage

To install this package, you should haveĀ pip on your system. We use pip, a python package installer, to download all kinds of packages used in python. Type these two lines to install nmaxmin:

pip install --upgrade pip
pip install nmaxmin

Now let’s see the workings of this module

To find nth maximum number:


We use nmaxmin.maxn(l, n) function for this purpose. It takes two parameters as input:

  1. l: array
  2. n: nth maximum number in l

Given the array arr, we find 4th maximum element from it.

arr = [4,6,8,10,45,82,64,245,29,1935,42,6,10]
nth = 4
maxx = nmaxmin.maxn(arr, nth) # returns nth = 4th maximum element from arr

Now, to validate the result of the module, we manually find the nth element using list.sort() function

print("{}th highest element according to nmaxmin in given array is: {}".format(nth, maxx))
arr.sort(reverse=True)
print("{}th highest element after sorting the given array is: {}".format(nth, arr[nth-1]))

Output:

4th highest element according to nmaxmin in given array is: 64

4th highest element after sorting the given array is: 64

To find nth minimum number:


We use nmaxmin.minn(l, n) function for this purpose. It takes two parameters as input:

  1. l: array
  2. n: nth minimum number in l

Given the array arr, we find 4th minimum element from it.

arr = [4,6,8,10,45,82,64,245,29,1935,42,6,10]
nth = 4
minn = nmaxmin.minn(arr, nth) # returns nth = 4th minimum element from arr

Just like earlier, we validate the results of the function using sort()

print("{}th lowest element according to nmaxmin in given array is: {}".format(nth, minn))
arr.sort()
print("{}th highest element after sorting the given array is: {}".format(nth, arr[nth-1]))

Output:

4th lowest element according to nmaxmin in given array is: 8 

4th highest element after sorting the given array is: 8

This module is pretty simple to implement. We hope that you found this article useful. Use nmaxmin module forĀ  your applications and let us know if this article was useful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *