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

Trong hướng dẫn này, bạn sẽ học cách viết chương trình Python để tìm số lớn nhất và nhỏ nhất trong một mảng.

Để tìm lớn nhất và nhỏ nhất trong một mảng, chúng tôi tìm thấy so sánh từng yếu tố với nhau và tìm thấy cái nào là lớn nhất và cái nào nhỏ nhất.

Trong chương trình này, chúng tôi đã lấy hai biến lớn nhất và nhỏ nhất sẽ giữ số lượng.largest and smallest which will hold number.

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

Làm thế nào để bạn tìm thấy số 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

Trong Python, có một hàm tích hợp tối đa () bạn có thể sử dụng để tìm số lớn nhất trong danh sách. Để sử dụng nó, hãy gọi Max () trên danh sách các số. Sau đó, nó trả về số lớn nhất trong danh sách đó.

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

1. Sắp xếp. Để tìm phần tử lớn nhất từ ​​mảng, một cách đơn giản là sắp xếp các phần tử theo thứ tự tăng dần. Sau khi sắp xếp, phần tử đầu tiên sẽ đại diện cho phần tử nhỏ nhất, phần tử tiếp theo sẽ là nhỏ thứ hai và diễn ra, phần tử cuối cùng sẽ là phần tử lớn nhất của mảng.

Làm thế nào để bạn tìm thấy số nhỏ nhất trong một mảng?

Nếu mảng được sắp xếp theo thứ tự giảm dần thì phần tử cuối cùng là phần tử nhỏ nhất, bạn có thể lấy nó bằng ARR [sizeofarray-1]. Nếu mảng không được sắp xếp thì bạn phải lặp lại mảng để có phần tử nhỏ nhất. Trong trường hợp phức tạp thời gian này là o (n), ở đây n là kích thước của mảng.

Làm thế nào để bạn tìm thấy giá trị lớn nhất và thấp 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 đầu vào () 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 một vòng lặp ..

Trong vòng lặp FOR, hãy nối từng 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 ..

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;

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;

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

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
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á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;

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 để bạn tìm thấy số lớn nhất trong một mảng trong Python?

Trong Python, có một hàm tích hợp tối đa () bạn có thể sử dụng để tìm số lớn nhất trong danh sách. Để sử dụng nó, hãy gọi Max () trên danh sách các số. Sau đó, nó trả về số lớn nhất trong danh sách đó.call the max() on a list of numbers. It then returns the greatest number in that list.

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

1. Sắp xếp.Để tìm phần tử lớn nhất từ mảng, một cách đơn giản là sắp xếp các phần tử theo thứ tự tăng dần.Sau khi sắp xếp, phần tử đầu tiên sẽ đại diện cho phần tử nhỏ nhất, phần tử tiếp theo sẽ là nhỏ thứ hai và diễn ra, phần tử cuối cùng sẽ là phần tử lớn nhất của mảng.arrange the elements in ascending order. After sorting, the first element will represent the smallest element, the next element will be the second smallest, and going on, the last element will be the largest element of the array.

Làm thế nào để bạn tìm thấy số nhỏ nhất trong một mảng?

Nếu mảng được sắp xếp theo thứ tự giảm dần thì phần tử cuối cùng là phần tử nhỏ nhất, bạn có thể lấy nó bằng ARR [sizeofarray-1].Nếu mảng không được sắp xếp thì bạn phải lặp lại mảng để có phần tử nhỏ nhất. Trong trường hợp phức tạp thời gian này là o (n), ở đây n là kích thước của mảng.arr[sizeOfArray-1]. If the array is not sorted then you have to iterate over the array to get the smallest element.In this case time complexity is O(n), here n is the size of array.

Làm thế nào để bạn tìm thấy giá trị lớn nhất và thấp 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 đầu vào () 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 một vòng lặp ..
Trong vòng lặp FOR, hãy nối từng 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 ..