Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

n-largest and n-smallest in list in python

import heapq

grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]

print(heapq.nlargest(3, grades))
print(heapq.nsmallest(4, grades))

[110, 95, 90]
[20, 25, 33, 38]
Comment

python code to find the smallest number from the list

# list of numbers
list1 = [10, 20, 9, 69, 9]

# sorting the list
list1.sort()
 
# printing the first element
print("Smallest element is:", list1[0])
Comment

find the largest and smallest numbers in a list

#Exercise 3 -  Find the largest and smallest number in a list
thislist = input("How many numbers do you want to enter:")
newlist = []
for x in range (0, int(thislist)):
 owl = int(input("Please Enter your numbers:"))
 newlist.append(owl)
print ("The max number entered is:", max(newlist))
print ("The min number entered is:", min(newlist))
Comment

find the largest and smallest numbers in a list

thislist = [677,8765,8765,876,470,754,6784,56789,7658,]
thislist.sort()
print ("The smallest number is: " + str(thislist[0]))
print ("The largest number is: " + str(thislist[-1]))
Comment

Python | Largest, Smallest, Second Largest, Second Smallest in a List

# Python prog to illustrate the following in a list
def find_len(list1):
    length = len(list1)
    list1.sort()
    print("Largest element is:", list1[length-1])
    print("Smallest element is:", list1[0])
    print("Second Largest element is:", list1[length-2])
    print("Second Smallest element is:", list1[1])
 
# Driver Code
list1=[12, 45, 2, 41, 31, 10, 8, 6, 4]
Largest = find_len(list1)
Comment

finding the largest or smallest N items in python

# Finding the largest or smallest N items using heapq
import heapq 

nums = [1, 8, 2, 23, 7, -4, 19, 23, 42, 38, 2]
print(heapq.nlargest(3, nums)) # 3 largest value 
print(heapq.nsmallest(3, nums)) # 3 smallest value 

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},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
Comment

PREVIOUS NEXT
Code Example
Python :: pandas reset index without adding column 
Python :: how do i print a list line by line in python 
Python :: resize interpolation cv2 
Python :: convert data type object to string python 
Python :: reverse geocode python 
Python :: cv2.threshold binary 
Python :: python chat application 
Python :: how to find empty rows of a dataset in python 
Python :: how to return an html file in flask 
Python :: seaborn define linewidth 
Python :: df count zeros 
Python :: python calculate derivative of function 
Python :: python float to 2 decimals 
Python :: load a Dictionary from File in Python Using the Load Function of the pickle Module 
Python :: show image with opencv2 
Python :: qradiobutton example 
Python :: numpy random.permutation 
Python :: if dict.values <= int 
Python :: assignment 7.1 python data structures 
Python :: how to tell if member is a bot discord.py 
Python :: handle queries in listview django 
Python :: fixed precision float python 
Python :: set camera width and height opencv python 
Python :: tkinter yes no dialogue box 
Python :: write data to using pickle 
Python :: python add element to array 
Python :: integer colomn to datetime pandas python 
Python :: select 2 cols from dataframe python pandas 
Python :: print list in reverse order python 
Python :: dense rank in pandas 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =