A nmaxmin module is that library of Python which helps you to find the nth maximum number and nth minimum number in a given list. It directly returns either the maximum number or minimum number with respect to the given index (n). Here the given list must not be in sorted order as it will traverse the list itself.
Note: Here’n’ must lies between ‘1’ and length of list, otherwise it will throw an exception .
Installing Library
This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal.
pip install nmaxmin
Functions of nmaxmin
- nmaxmin.maxn(l, n): This function takes two parameters as input (one is a list and the second is index ‘n’).It will return the nth maximum number from the list as output.
Example :
# Importing maxn function
# From nmaxmin Library
from
nmaxmin
import
nmaxmin
l
=
[
10
,
25
,
40
,
30
,
15
,
50
,
65
,
70
]
a
=
nmaxmin.maxn(l,
3
)
print
(
"The 2nd maximum number in a list l is "
, a)
a
=
nmaxmin.maxn(l,
5
)
print
(
"The 5th maximum number in a list l is "
, a)
a
=
nmaxmin.maxn(l,
8
)
print
(
"The 8th maximum number in a list l is "
, a)
Output:
The 2nd maximum number in a list l is 50 The 5th maximum number in a list l is 30 The 8th maximum number in a list l is 10 # Importing minn function
- nmaxmin.minn(l, n): This function takes two parameters as input (one is a list and the second is index ‘n’).It will return the nth minimum number from the list as output.
Example :
# Importing minn function
# From nmaxmin Library
from
nmaxmin
import
nmaxmin
l
=
[
10
,
25
,
40
,
30
,
15
,
50
,
65
,
70
]
a
=
nmaxmin.minn(l,
5
)
print
(
"The 5th minimum number in a list l is "
, a)
a
=
nmaxmin.minn(l,
2
)
print
(
"The 2nd minimum number in a list l is "
, a)
a
=
nmaxmin.minn(l,
1
)
print
(
"The 1st minimum number in a list l is "
, a)
Output:
The 5th minimum number in a list l is 40 The 2nd minimum number in a list l is 15 The 1st minimum number in a list l is 10