Hướng dẫn how do you find the smallest and largest numbers in python? - làm thế nào để bạn tìm thấy các số nhỏ nhất và lớn nhất trong python?

Xin chào mọi người, đây là một chương trình Python tìm ra con số nhỏ nhất và lớn nhất trong danh sách.

Số lượng nhỏ nhất và lớn nhất trong danh sách - Lập trìnhDInpython.com

Ở đây chúng tôi sử dụng 2 hàm được xác định trước Min [] và Max [] kiểm tra số nhỏ nhất và lớn nhất trong danh sách tương ứng.

Nhiệm vụ :

Để tìm số lớn nhất và nhỏ nhất trong một danh sách.

Cách tiếp cận :

  • Đọc số đầu vào yêu cầu độ dài của danh sách bằng cách sử dụng input[] hoặc raw_input[].
  • Khởi tạo một danh sách trống lst = [].
  • Đọc từng số bằng cách sử dụng for loop.
  • Trong vòng lặp FOR, nối mỗi số vào danh sách.
  • Bây giờ chúng tôi sử dụng hàm được xác định trước
    Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
    Output : 
    Largest element is: 45
    Smallest element is: 2
    Second Largest element is: 41
    Second Smallest element is: 4
    0 để tìm phần tử lớn nhất trong danh sách.
  • Tương tự, chúng tôi sử dụng một chức năng được xác định trước khác
    Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
    Output : 
    Largest element is: 45
    Smallest element is: 2
    Second Largest element is: 41
    Second Smallest element is: 4
    1 để tìm phần tử nhỏ nhất trong danh sách.

Chương trình :

lst = []
num = int[input['How many numbers: ']]
for n in range[num]:
numbers = int[input['Enter number ']]
lst.append[numbers]
print["Maximum element in the list is :", max[lst], "\nMinimum element in the list is :", min[lst]]

Đầu ra:

Số lượng nhỏ nhất và lớn nhất trong danh sách - Lập trìnhDInpython.com

Vì, không giống như các ngôn ngữ lập trình khác, Python không có mảng, thay vào đó, nó có danh sách. Sử dụng danh sách dễ dàng hơn và thoải mái hơn để làm việc so với các mảng. Hơn nữa, các chức năng khổng lồ của Python, làm cho nhiệm vụ dễ dàng hơn. Vì vậy, sử dụng các kỹ thuật này, hãy để cố gắng tìm các phạm vi khác nhau của số trong một danh sách nhất định. & NBSP;

Examples:

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12

Cách tiếp cận số 1: Cách tiếp cận rất đơn giản. Python cho phép chúng tôi sắp xếp một danh sách bằng hàm Danh sách []. Sử dụng điều này, chúng ta có thể tìm thấy các phạm vi số khác nhau trong một danh sách, từ vị trí của chúng, sau khi được sắp xếp. Giống như vị trí đầu tiên phải chứa phần tử nhỏ nhất và phần tử cuối cùng phải là lớn nhất. & NBSP;The approach is simple. Python allows us to sort a list using the list[] function. Using this we can find various ranges of numbers in a list, from their position, after being sorted. Like the first position must contain the smallest and the last element must be the greatest. 

Python3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
0

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
3
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
5
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
7
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Các

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
7
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input[]0
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input[]2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input[]4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input[]6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input[]8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5raw_input[]0raw_input[]1

raw_input[]2

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 raw_input[]4

Output:

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4

Làm thế nào để bạn tìm thấy số nhỏ nhất và lớn nhất trong một mảng trong Python?Below is another traditional method to do the following calculation. The algorithm is simple, we take a number and compare it with all other numbers present in the list and get the largest, smallest, second largest, and second smallest element. 

Python3

Thuật toán để tìm các số nhỏ nhất và lớn nhất trong một mảng.

Nhập các phần tử mảng ..

Khởi tạo nhỏ = lớn = mảng [0].

Lặp lại từ i = 2 đến n ..

if [mảng [i]> lớn].

lớn = mảng [i].

if [mảng [i] lớn].

lớn = mảng [i].
if [mảng [i]

Bài Viết Liên Quan

Chủ Đề