Hướng dẫn largest and smallest number in array python - số lớn nhất và nhỏ nhất trong python mảng

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

def find_len[list1]:

    length =

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 = [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
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
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
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 = [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
9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
0
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
5
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
7
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
7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1

Các

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
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
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
0
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1

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
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
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
8def3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8find_len[list1]:1
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8find_len[list1]:3find_len[list1]:4

find_len[list1]:5= find_len[list1]:7

Output:

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

Cách tiếp cận số 2: Dưới đây là một phương pháp truyền thống khác để thực hiện tính toán sau. Thuật toán rất đơn giản, chúng tôi lấy một số và so sánh nó với tất cả các số khác có trong danh sách và nhận phần tử nhỏ nhất, nhỏ nhất, lớn nhất và thứ hai nhỏ nhất và thứ hai. & NBSP;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

def find_len[list1]:9    0

        2=     4

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7find_len[list1]:4

        8=     4

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7find_len[list1]:4

    length 4= length 6

    length 8= length 6

    =2 =3=4     4

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
0=7

=8=9

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
00

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
01length 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
04

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
01    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
08

=8

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
10 length 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
7 length 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
14
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
15

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
01length 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
08

=8=9

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
22

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
01length 8=
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
26

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
01    8=
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
08

=8

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
10 length 8
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 length 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
14
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
37

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
01length 8=
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
08

    

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
46

    

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
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
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
51

    

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
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
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
56

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
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
61

Các

find_len[list1]:9

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
1

Không gian phụ trợ: O [1]

Output:

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

Cách tiếp cận số 3: Nhiệm vụ này có thể được thực hiện bằng các phương thức danh sách tối đa và pop. Chúng ta có thể tìm thấy phần tử lớn nhất và nhỏ nhất của danh sách bằng phương pháp tối đa và tối thiểu sau khi nhận được phần tử tối đa và tối đa vượt qua các phần tử từ danh sách và một lần nữa sử dụng phần tử tối đa và tối đa để có phần tử nhỏ thứ hai và nhỏ thứ hai. & NBSP;This task can be performed using max and pop methods of list. We can find largest and smallest element of list using max and min method after getting min and max element pop outs the elements from list and again use min and max element to get the second largest and second smallest element. 

Python3

def

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
86

    

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
88=
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
90
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
91

    

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
93

    

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
95=
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
90
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
1

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
00____9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
022

    

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

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
07____9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
02
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
91

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
13
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
14
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
15

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
13
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
19
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
20

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
13
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
24
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
25

    

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
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
13
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
29
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
30

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
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
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
8def3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8def9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8find_len[list1]:1
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8find_len[list1]:3find_len[list1]:4

find_len[list1]:5= find_len[list1]:7

Output:

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

Làm thế nào để tôi tìm thấy số lớn nhất và nhỏ nhất trong Python sử dụng cho 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 Max [] để 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 hàm được xác định trước khác [] để tìm phần tử nhỏ nhất trong danh sách.

Làm thế nào để bạn tìm thấy lớn nhất trong một mảng trong Python?

ALGORITHM:..
Bước 1: Khai báo và khởi tạo một mảng ..
Bước 2: Lưu trữ phần tử đầu tiên trong biến tối đa ..
Bước 3: Vòng lặp qua mảng từ 0 đến chiều dài của mảng và so sánh giá trị tối đa với các phần tử của mảng ..
Bước 4: Nếu bất kỳ phần tử nào lớn hơn tối đa, Max sẽ giữ giá trị của phần tử đó ..

Làm thế nào để bạn kích thước một mảng trong Python?

Sử dụng phương thức Len [] để trả về chiều dài của một mảng [số lượng phần tử trong một mảng]. [the number of elements in an array].

Chủ Đề