Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

nlargest heapq

heapq.nlargest(3, li1)
Comment

nlargest heapq

nlargest in heapq
nlargest(k, iterable, key = fun) :- 
This function is used to return the k largest elements from the iterable 
specified and satisfying the key if mentioned.

code:
'''
# Python code to demonstrate working of
# nlargest() and nsmallest()

# importing "heapq" to implement heap queue
import heapq

# initializing list
li1 = [6, 7, 9, 4, 3, 5, 8, 10, 1]

# using heapify() to convert list into heap
heapq.heapify(li1)

# using nlargest to print 3 largest numbers
# prints 10, 9 and 8
print("The 3 largest numbers in list are : ",end="")
print(heapq.nlargest(3, li1))

# using nsmallest to print 3 smallest numbers
# prints 1, 3 and 4
print("The 3 smallest numbers in list are : ",end="")
print(heapq.nsmallest(3, li1))

'''


output : 
'''
The 3 largest numbers in list are : [10, 9, 8]
The 3 smallest numbers in list are : [1, 3, 4]
'''
Comment

PREVIOUS NEXT
Code Example
Python :: Python Creating string from a timestamp 
Python :: python get first day of year 
Python :: orderd set in python 
Python :: df only take 2 columns 
Python :: pycairo 
Python :: python simple input popup 
Python :: ordered dictionary python 
Python :: python manage.py collectstatic --noinput 
Python :: console.log() python 
Python :: sys.path[0] 
Python :: python odbc access database 
Python :: python run command and read output 
Python :: fixed precision float python 
Python :: python regex match words 
Python :: pandas slicing from one column to another 
Python :: generate new secret key django 
Python :: pandas merge certain columns 
Python :: 2 distinct numbers random number generator python 
Python :: radix sort python 
Python :: delete directory if exists python 
Python :: draw bounding box on image python opencv 
Python :: shutil move file 
Python :: python get date from unix timestamp 
Python :: xa python 
Python :: django objects.create() 
Python :: Plot regression line from sklearn 
Python :: print map object python 
Python :: excel get unique values from column formula 
Python :: python print numbers 1 to 10 in one line 
Python :: python convert string to float array 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =