Hướng dẫn how do you check if an item exists in a list python? - làm cách nào để kiểm tra xem một mục có tồn tại trong danh sách python không?

Danh sách này là một thùng chứa quan trọng trong Python vì nó lưu trữ các yếu tố của tất cả các loại dữ liệu dưới dạng bộ sưu tập. Kiến thức về các hoạt động danh sách nhất định là cần thiết cho lập trình hàng ngày. Bài viết này thảo luận về cách nhanh nhất để kiểm tra xem giá trị có tồn tại trong danh sách hay không sử dụng Python.

Example:

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
Input: 7  # Check if 7 exist or not.
Output: False

Phương pháp 1: Phương pháp ngây thơ

Trong phương pháp ngây thơ, người ta dễ dàng sử dụng một vòng lặp lặp qua tất cả các yếu tố để kiểm tra sự tồn tại của phần tử đích. Đây là cách đơn giản nhất để kiểm tra sự tồn tại của phần tử trong danh sách. Python là cách thông thường nhất để kiểm tra xem một yếu tố có tồn tại trong danh sách hay không. Cách cụ thể này trả về đúng nếu một phần tử tồn tại trong danh sách và sai nếu phần tử không tồn tại trong danh sách. Danh sách không cần phải được sắp xếp để thực hành phương pháp kiểm tra này.

Ví dụ 1: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng câu lệnh if-else không Check if an element exists in the list using the if-else statement

Python3

Các

Element Exists
4
Input: 7  # Check if 7 exist or not.
Output: False
0
Element Exists
6

Element Exists
7
Element Exists
8
Element Exists
9
Element Exists
0

Element Exists
1
Element Exists
2
Element Exists
3
Element Exists
4
Element Exists
5

Element Exists
6
Element Exists
7

Element Exists
1
Element Exists
2
Element Exists
3
Does string contain any list element : True
1
Element Exists
5

Ví dụ 2: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng Loop & NBSP hay không;Check if an element exists in the list using a loop 

Python3

Does string contain any list element : True
3
Input: 7  # Check if 7 exist or not.
Output: False
0
Does string contain any list element : True
5
Input: 7  # Check if 7 exist or not.
Output: False
2
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
4
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
6
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
8
Input: 7  # Check if 7 exist or not.
Output: False
3__

Checking if 15 exists in list
Yes, 15 exists in list
8
Element Exists
8
Element Exists
9
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
1

Element Exists
1
Element Exists
7
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
44____1010
Element Exists
22

Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
9
Element Exists
2
Element Exists
3
Checking if 15 exists in list
Yes, 15 exists in list
2
Element Exists
5

Output:

Element Exists

Ví dụ 3: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng trên mạng trong & NBSP hay không;Check if an element exists in the list using “in” 

Python3

Does string contain any list element : True
3
Input: 7  # Check if 7 exist or not.
Output: False
0
Does string contain any list element : True
5
Input: 7  # Check if 7 exist or not.
Output: False
2
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
4
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
6
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
8
Input: 7  # Check if 7 exist or not.
Output: False
3__

Checking if 15 exists in list
Yes, 15 exists in list
8
Element Exists
8
Element Exists
9
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
1

Element Exists
1
Element Exists
7
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
44____1010
Element Exists
22

Output:

Element Exists

Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
9
Element Exists
2
Element Exists
3
Checking if 15 exists in list
Yes, 15 exists in list
2
Element Exists
5
Check if an element exists in the list using any() function

Python3

Does string contain any list element : True
3
Input: 7  # Check if 7 exist or not.
Output: False
0
Does string contain any list element : True
5
Input: 7  # Check if 7 exist or not.
Output: False
2
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
4
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
6
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
8
Input: 7  # Check if 7 exist or not.
Output: False
3__

Checking if 15 exists in list
Yes, 15 exists in list
8
Element Exists
8
Element Exists
9
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
1

Element Exists
1
Element Exists
7
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
44____1010
Element Exists
22

Output:

Does string contain any list element : True

Checking if 4 exists in list ( using set() + in) : Element Exists Checking if 4 exists in list ( using sort() + bisect_left() ) : Element Exists9Element Exists2Element Exists3Checking if 15 exists in list Yes, 15 exists in list2Element Exists5count()

Ví dụ 3: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng trên mạng trong & NBSP hay không;

Python3

Element Exists
7
Element Exists
3
Element Exists
2
Element Exists
9 lst3

Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
49
Element Exists
5

Element Exists
1
Element Exists
2
Element Exists
3
Checking if 15 exists in list
Yes, 15 exists in list
2
Element Exists
5

Ví dụ 4: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng hàm bất kỳ () nào không

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
63
Element Exists
5

Element Exists
6
Element Exists
7

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
70
Element Exists
5

Output:

Checking if 15 exists in list
Yes, 15 exists in list

Input: 7 # Check if 7 exist or not. Output: False14Input: 7 # Check if 7 exist or not. Output: False0 Input: 7 # Check if 7 exist or not. Output: False16Input: 7 # Check if 7 exist or not. Output: False17Element Exists9 Does string contain any list element : True3Checking if 15 exists in list Yes, 15 exists in list8

Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
26
Input: 7  # Check if 7 exist or not.
Output: False
27
Input: 7  # Check if 7 exist or not.
Output: False
28
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
30
Input: 7  # Check if 7 exist or not.
Output: False
31

Phương pháp 2: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng Count ()The bisect function will only state the position of where to insert the element but not the details about if the element is present or not.

Chúng ta có thể sử dụng phương thức danh sách Python được xây dựng, Count (), để kiểm tra xem phần tử được truyền có tồn tại trong danh sách không. Nếu phần tử được truyền tồn tại trong danh sách, phương thức đếm () sẽ hiển thị số lần nó xảy ra trong toàn bộ danh sách. Nếu đó là một số dương khác không, nó có nghĩa là một yếu tố tồn tại trong danh sách. Thể hiện để kiểm tra sự tồn tại của các yếu tố trong danh sách bằng cách sử dụng Count ().

Python3

Does string contain any list element : True
3
Input: 7  # Check if 7 exist or not.
Output: False
0
Does string contain any list element : True
5
Input: 7  # Check if 7 exist or not.
Output: False
35
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
37
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
39
Input: 7  # Check if 7 exist or not.
Output: False
3__

Input: 7  # Check if 7 exist or not.
Output: False
51
Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
53
Input: 7  # Check if 7 exist or not.
Output: False
37
Element Exists
5

Các

Element Exists
2
Element Exists
3
Element Exists
08
Element Exists
5

Input: 7  # Check if 7 exist or not.
Output: False
76____10
Element Exists
1212

Element Exists
7
Element Exists
2
Element Exists
9
Element Exists
17

Element Exists
1
Element Exists
2
Element Exists
3
Checking if 15 exists in list
Yes, 15 exists in list
2
Element Exists
5

Element Exists
2
Element Exists
3
Element Exists
25
Element Exists
5

Element Exists
27

Is

Element Exists
1
Element Exists
2
Element Exists
3
Checking if 15 exists in list
Yes, 15 exists in list
2
Element Exists
5

Element Exists
6
Element Exists
7

Element Exists
1
Element Exists
2
Element Exists
3
Element Exists
46
Element Exists
5

Output:

Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists

Element Exists2Element Exists3Element Exists25Element Exists5

Python3

Is

Phương pháp 4: Sử dụng phương thức Find ()

Element Exists
67
Input: 7  # Check if 7 exist or not.
Output: False
0
Element Exists
69
Element Exists
3
Element Exists
71
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
28
Element Exists
74

Element Exists
75
Input: 7  # Check if 7 exist or not.
Output: False
0
Element Exists
77
Element Exists
78

Does string contain any list element : True
3
Input: 7  # Check if 7 exist or not.
Output: False
0
Does string contain any list element : True
5
Input: 7  # Check if 7 exist or not.
Output: False
35
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
37
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
39
Input: 7  # Check if 7 exist or not.
Output: False
3__

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
63
Element Exists
5

Element Exists
6
Element Exists
7

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
70
Element Exists
5

Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
49
Element Exists
5

Checking if 15 exists in list
Yes, 15 exists in list

Element Exists7 Element Exists80Element Exists81Element Exists82___

Đầu ra

Python3

Phương pháp 5: Sử dụng hàm bộ đếm ()

Is

Phương pháp 4: Sử dụng phương thức Find ()

Element Exists
7
Element Exists
22
Input: 7  # Check if 7 exist or not.
Output: False
37
Element Exists
24
Input: 7  # Check if 7 exist or not.
Output: False
58
Checking if 4 exists in list ( using set() + in) : 
Element Exists
Checking if 4 exists in list ( using sort() + bisect_left() ) : 
Element Exists
8

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
63
Element Exists
5

Element Exists
6
Element Exists
7

Element Exists
1
Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
70
Element Exists
5

Element Exists
2
Element Exists
3
Input: 7  # Check if 7 exist or not.
Output: False
49
Element Exists
5

Yes, 15 exists in list