


Implementation code for finding the largest or smallest N elements in python
This article brings you the implementation code for finding the largest or smallest N elements in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Requirements
We want to find the largest or smallest N elements in a certain set
2.Solution
There are two functions in the heapq module: nlargest() and nsmallest()
Code:
import heapq nums=[1,444,66,77,34,67,2,6,8,2,4,9,556] print(heapq.nlargest(3,nums)) print(heapq.nsmallest(3,nums))
Result:
[556, 444, 77] [1, 2, 2]
These two Each function can accept a parameter key, allowing them to work on more complex data structures:
Code:
import heapq portfolio=[ {'name':'IBM','shares':100,'price':91.1}, {'name':'AAPL','shares':50,'price':543.22}, {'name':'FB','shares':200,'price':21.09}, {'name':'HPQ','shares':35,'price':31.75}, {'name':'YHOO','shares':45,'price':16.35}, ] cheap=heapq.nsmallest(3,portfolio,key=lambda s:s['price']) expensive=heapq.nlargest(3,portfolio,key=lambda s:s['price']) print(cheap) print(expensive)
Result:
[{'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}] [{'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}]
If it is just simple To find the smallest or largest element (N=1), it will be faster to use min() and max().
The above is the detailed content of Implementation code for finding the largest or smallest N elements in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Many developers rely on PyPI (PythonPackageIndex)...

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...
