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
not exist5
Input: 7 # Check if 7 exist or not. Output: False1
not exist7
not exist8
not exist9
Element Exists0
Element Exists1
Element Exists2
Element Exists3
Element Exists4
Element Exists5
Element Exists6
Element Exists7
Element Exists8
Element Exists2
Element Exists3
Element Exists4
Element Exists2
Element Exists6
Output:
not exist
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
Element Exists4___
Does string contain any list element : True9
not exist9
Element Exists0
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists2
not exist8
Checking if 15 exists in list Yes, 15 exists in list5
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False1
not exist3
Checking if 15 exists in list Yes, 15 exists in list9
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists0
Element Exists3
Element Exists4
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists3
Element Exists6
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
Element Exists4___
Does string contain any list element : True9
not exist9
Element Exists0
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists2
Element Exists3
Element Exists4
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists3
Element Exists6
Output:
Element Exists
Element Exists
2not exist
8Checking if 15 exists in list
Yes, 15 exists in list
5Input: 7 # Check if 7 exist or not.
Output: False
1Input: 7 # Check if 7 exist or not.
Output: False
1 not exist
3Checking if 15 exists in list
Yes, 15 exists in list
9Check if an element exists in the list using any[] function
Python3
Element Exists4___
Does string contain any list element : True9
not exist9
Element Exists0
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists2
not exist8
Checking if 15 exists in list Yes, 15 exists in list5
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False1
not exist3
Checking if 15 exists in list Yes, 15 exists in list9
Output:
Does string contain any list element : True
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;count[]
not exist8
Element Exists4
not exist3
Element Exists0
Yes, 15 exists in list4
Python3
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 Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False50
Element Exists6
Input: 7 # Check if 7 exist or not. Output: False15
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False17
Input: 7 # Check if 7 exist or not. Output: False18
Element Exists0
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False27
Input: 7 # Check if 7 exist or not. Output: False28
Input: 7 # Check if 7 exist or not. Output: False29
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False31
Input: 7 # Check if 7 exist or not. Output: False32
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False64
Element Exists6
Element Exists7
Element Exists8
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False71
Element Exists6
Output:
Checking if 15 exists in list Yes, 15 exists in list
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 []
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 [].
Element Exists4Input: 7 # Check if 7 exist or not. Output: False1Element Exists6Input: 7 # Check if 7 exist or not. Output: False36Input: 7 # Check if 7 exist or not. Output: False4Input: 7 # Check if 7 exist or not. Output: False38__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.
Input: 7 # Check if 7 exist or not. Output: False52
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False54
Input: 7 # Check if 7 exist or not. Output: False38
Element Exists6
Python3
not exist8
Input: 7 # Check if 7 exist or not. Output: False58
Input: 7 # Check if 7 exist or not. Output: False59
Element Exists8
Phương pháp 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 sort + bicect_left + set
Các
Element Exists3
Element Exists4
not exist09
Element Exists6
Input: 7 # Check if 7 exist or not. Output: False77
Input: 7 # Check if 7 exist or not. Output: False1
not exist13
not exist14
not exist8
not exist3
Element Exists0
not exist18
Element Exists2
Element Exists3
Element Exists4
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists3
Element Exists6
Element Exists3
Element Exists4
not exist26
Element Exists6
not exist28
not exist8
not exist30
not exist3
not exist32
Input: 7 # Check if 7 exist or not. Output: False1
not exist34
not exist3__
Element Exists2
Element Exists3
Element Exists4
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists3
Element Exists6
Element Exists7
Element Exists8
Element Exists2
Element Exists3
Element Exists4
not exist47
Element Exists6
Output:
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists
not exist
28
Python3
not exist8
not exist30
not exist3
not exist32
Input: 7 # Check if 7 exist or not. Output: False1
not exist34
not exist3__
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False50
Element Exists6
not exist68
Input: 7 # Check if 7 exist or not. Output: False1
not exist70
Element Exists4
not exist72
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False29
not exist75
not exist76
Input: 7 # Check if 7 exist or not. Output: False1
not exist78
not exist79
Phương pháp 4: Sử dụng phương thức Find []
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False64
Element Exists6
Element Exists7
Element Exists8
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False71
Element Exists6
Element Exists
4Input: 7 # Check if 7 exist or not.
Output: False
1 Element Exists
6Input: 7 # Check if 7 exist or not.
Output: False
36Input: 7 # Check if 7 exist or not.
Output: False
4Input: 7 # Check if 7 exist or not.
Output: False
38__
Checking if 15 exists in list Yes, 15 exists in list
Is
Đầu ra
Python3
Phương pháp 5: Sử dụng hàm bộ đếm []
not exist8
not exist30
not exist3
not exist32
Input: 7 # Check if 7 exist or not. Output: False1
not exist34
not exist3__
Phương pháp 4: Sử dụng phương thức Find []
not exist8
Element Exists23
Input: 7 # Check if 7 exist or not. Output: False38
Element Exists25
Input: 7 # Check if 7 exist or not. Output: False59
Checking if 15 exists in list Yes, 15 exists in list9
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False64
Element Exists6
Element Exists7
Element Exists8
Element Exists2
Element Exists3
Element Exists4
Input: 7 # Check if 7 exist or not. Output: False71
Element Exists6
Element Exists
4Input: 7 # Check if 7 exist or not.
Output: False
1 Element Exists
6Input: 7 # Check if 7 exist or not.
Output: False
36Input: 7 # Check if 7 exist or not.
Output: False
4Input: 7 # Check if 7 exist or not.
Output: False
38__
Yes, 15 exists in list