Python程序从列表中查找N个最大和最小的元素

本文介绍了两种从Python列表中找出N个最大和最小元素的方法:一是自定义函数,通过循环和列表操作实现;二是利用heapq模块的nlargest和nsmallest函数。这两种方法适用于N远小于列表长度的情况。文章还提到了当N为1时,使用min和max函数更为高效。

Here, we learn how to find out the N largest and smallest elements from the list? Where, list and N given by the user, N may be any value but less than the list length.

在这里,我们学习如何从列表中找出N个最大和最小的元素? 其中, 列表和用户给定的N , N可以是任何值,但小于列表长度。

Description:

描述:

There are two ways,

有两种方法,

1. By defining a function:

1.通过定义一个函数:

Procedure:

程序:

  1. Define the function name largest_ele and smallest_ele.

    定义函数名largest_ele和smallest_ele。

  2. Pass two arguments in a function (l,n) : l is the list and n is the number of elements.

    在函数(l,n)中传递两个参数: l是列表, n是元素数。

  3. Run the for loop n times

    运行for循环n次

  4. In the loop find the maximum of the given list and append it to another list

    在循环中找到给定列表的最大值,然后将其追加到另一个列表

  5. And after appending to another list remove the maximum element from the list

    在追加到另一个列表后,从列表中删除最大元素

By the inbuilt module heapq module

通过内置模块heapq模块

If you are looking for the N smallest or largest items and N is small compared to the overall size of the collection, these functions provide superior performance.

如果您正在寻找N个最小或最大的项目,并且N与集合的整体大小相比较小,则这些功能可提供卓越的性能。

  1. Import the heapq module

    导入heapq模块

  2. Give the list

    给出清单

  3. Now use the function heapq.nlargest(n,l) and heapq.nsmallest(n,l) from the module to find the largest and the smallest numbers.

    现在,使用模块中的函数heapq.nlargest(n,l)和heapq.nsmallest(n,l)查找最大和最小的数字。

Python code:

Python代码:

# N largest and smallest element in a list 
# by function and by the help of heapq module

#function to find n largest element
def largest_ele(l,n): 
    s=[]
    for i in range(n):
        s.append(max(l)) #append max of list in a new list
        l.remove(max(l)) #remove max of list from the list
    print('by largest_ele function: ',s)


	#function to find n largest element
def smallest_ele(m,n): 
    t=[]
    for i in range(n):
        t.append(min(m))#append min of list in a new list
        m.remove(min(m))#remove min of list from the list
    print('by smallest_ele function: ',t)


l=[2,4,6,8,10]
m=[0,1,2,3,4,5,6]
n=2
largest_ele(l,n)
smallest_ele(m,n)

# using the inbuilt module function 
# heapq.nlargest and heapq.nsmallest
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print('BY heapq.nlargest: ',heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print('BY heapq.nsmallest: ',heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]

Output

输出量

by largest_ele function:  [10, 8]
by smallest_ele function:  [0, 1]
BY heapq.nlargest:  [42, 37, 23]
BY heapq.nsmallest:  [-4, 1, 2]

Note: The nlargest() and nsmallest() functions are most appropriate if you are trying to find a relatively small number of items. If you are simply trying to find the single smallest or largest item (N=1), it is faster to use min() and max().

注意:如果要查找相对较少的项,则nlargest()和nsmallest()函数最合适。 如果您只是想查找单个最小或最大项(N = 1),则使用min()max()更快。

翻译自: https://www.includehelp.com/python/find-n-largest-or-smallest-elements-from-the-list.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值