Python nmaxmin 模块
nmaxmin模块是Python的库,它可以帮助您找到给定列表中的第 n 个最大数和第 n 个最小数。它直接返回关于给定索引 (n) 的最大数或最小数。这里给定的列表不能按排序顺序,因为它将遍历列表本身。
注意:这里的'n'必须介于'1'和列表长度之间,否则会抛出异常。
安装库
这个模块没有内置在Python中。您需要在外部安装它。要安装此模块,请在终端中键入以下命令。
pip install nmaxmin
nmaxmin 的函数
- nmaxmin.maxn(l, n) :此函数将两个参数作为输入(一个是列表,第二个是索引'n')。它将返回列表中的第 n 个最大数作为输出。
例子 :
# 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)
输出:
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) :此函数将两个参数作为输入(一个是列表,第二个是索引'n')。它将返回列表中的第 n 个最小数字作为输出。
例子 :
# 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)
输出:
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