Hướng dẫn how do you find the smallest element in a list python? - làm thế nào để bạn tìm thấy phần tử nhỏ nhất trong danh sách python?

Chúng tôi được cung cấp một danh sách các số và nhiệm vụ của chúng tôi là viết một chương trình Python để tìm số nhỏ nhất trong danh sách đã cho. Đối với chương trình sau đây, chúng tôi có thể sử dụng các phương thức khác nhau bao gồm phương thức Min tích hợp, sắp xếp mảng & nbsp; và trả về phần tử cuối cùng, v.v., ví dụ: & nbsp;
Example: 

Input : list1 = [10, 20, 4]
Output : 4

Input : list2 = [20, 10, 20, 1, 100]
Output : 1

Sắp xếp danh sách để tìm số nhỏ nhất trong danh sách

Theo thứ tự tăng dần

Ở đây viết một chương trình Python nơi chúng tôi đang sắp xếp toàn bộ danh sách và sau đó trả lại phần tử đầu tiên vì nó sẽ là yếu tố nhỏ nhất có trong danh sách.

Python3

list1

smallest element is: 4
0
smallest element is: 4
1
smallest element is: 4
2
smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
3
smallest element is: 4
6
smallest element is: 4
3
smallest element is: 4
8
smallest element is: 4
3__

smallest element is: 4
2

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
5
smallest element is: 4
6
smallest element is: 4
7
smallest element is: 4
8

Output: 

smallest element is: 4

Thứ tự giảm dần

Ở đây chúng tôi đang sắp xếp bằng cách sử dụng hàm sort () toàn bộ danh sách và sau đó trả về phần tử cuối cùng vì nó sẽ là phần tử nhỏ nhất có trong danh sách.

Quảng cáo

Python3

list1

smallest element is: 4
0
smallest element is: 4
1
smallest element is: 4
2
smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
3
smallest element is: 4
6
smallest element is: 4
3
smallest element is: 4
8
smallest element is: 4
3__

Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11
2
smallest element is: 4
0
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11
4
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11
5

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
5
smallest element is: 4
6
List: 23,-1,45,22.6,78,100,-5
0
List: 23,-1,45,22.6,78,100,-5
1
smallest element is: 4
8

Output:

smallest element is: 4

Thứ tự giảm dần

Ở đây chúng tôi đang sắp xếp bằng cách sử dụng hàm sort () toàn bộ danh sách và sau đó trả về phần tử cuối cùng vì nó sẽ là phần tử nhỏ nhất có trong danh sách.

Python3

Quảng cáo

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
5
smallest element is: 4
3
1
0
1
1

Output: 

Smallest element is: 1

Sử dụng phương thức min () & nbsp; để tìm số nhỏ nhất trong danh sách

Python3

Ở đây chúng tôi đang sử dụng phương thức Min và sau đó trả về phần tử nhỏ nhất có trong danh sách.

list1

smallest element is: 4
0
smallest element is: 4
1
smallest element is: 4
2
smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
3
List: 23,-1,45,22.6,78,100,-5
1
smallest element is: 4
3
smallest element is: 4
8
smallest element is: 4
3__

Tìm phần tử danh sách tối thiểu cho danh sách do người dùng xác định

list1 ____10

1
4

list1 3

smallest element is: 4
03

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
5
smallest element is: 4
3
1
0
1
1

Output: 

Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11

Python3

1
3
1
4
1
5
1
6__

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
26
smallest element is: 4
27

list1 3list1 4

smallest element is: 4
0
1
7
smallest element is: 4
4
1
9
smallest element is: 4
4
smallest element is: 4
00
1
2

Tìm phần tử nhỏ nhất trong danh sách so sánh mọi yếu tố

smallest element is: 4
10
smallest element is: 4
0
smallest element is: 4
122.

smallest element is: 4
28
smallest element is: 4
0
smallest element is: 4
30
smallest element is: 4
7
smallest element is: 4
1

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
49
smallest element is: 4
50

Input: 

List: 23,-1,45,22.6,78,100,-5

Output:  

The list is ['23', '-1', '45', '22.6', '78', '100','-5']
The smallest element in the list is  -5

13 1415 16smallest element is: 44smallest element is: 438smallest element is: 439

list1 3

smallest element is: 4
41
smallest element is: 4
42

Python3

smallest element is: 4
43
smallest element is: 4
28
smallest element is: 4
0
smallest element is: 4
46

Sử dụng chức năng Lambda để tìm số nhỏ nhất trong danh sách

Output:

1

Ở đây chúng tôi đang sử dụng chức năng Lambda để in số nhỏ nhất có trong danh sách.

smallest element is: 4
51
smallest element is: 4
0
smallest element is: 4
1
smallest element is: 4
4
smallest element is: 4
3__12

Python3

smallest element is: 4
43
smallest element is: 4
28
smallest element is: 4
0
smallest element is: 4
46

Sử dụng chức năng Lambda để tìm số nhỏ nhất trong danh sách

smallest element is: 4
3
smallest element is: 4
96

Output:

1

Ở đây chúng tôi đang sử dụng chức năng Lambda để in số nhỏ nhất có trong danh sách.

smallest element is: 4
51
smallest element is: 4
0
smallest element is: 4
1
smallest element is: 4
4
smallest element is: 4
3__12

Các

Sử dụng hàm liệt kê để tìm số nhỏ nhất trong danh sách

Ở đây chúng tôi đang lặp lại trong danh sách bằng cách sử dụng hàm liệt kê () và trả về phần tử cuối cùng.

smallest element is: 4
3
smallest element is: 4
4
smallest element is: 4
00
smallest element is: 4
4
1
0
smallest element is: 4
19