Python nmaxmin Module

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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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) 

    chevron_right

    
    

    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
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.