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 Exists4
Input: 7 # Check if 7 exist or not. Output: False0
Element Exists6
Element Exists7
Element Exists8
Element Exists9
Element Exists0
Element Exists1
Element Exists2
Element Exists3
Element Exists4
Element Exists5
Element Exists6
Element Exists7
Element Exists1
Element Exists2
Element Exists3
Does string contain any list element : True1
Element Exists5
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 : True3
Input: 7 # Check if 7 exist or not. Output: False0
Does string contain any list element : True5
Input: 7 # Check if 7 exist or not. Output: False2
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False8
Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8
Element Exists8
Element Exists9
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists1
Element Exists1
Element Exists7
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists44____1010
Element Exists22
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists9
Element Exists2
Element Exists3
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists5
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 : True3
Input: 7 # Check if 7 exist or not. Output: False0
Does string contain any list element : True5
Input: 7 # Check if 7 exist or not. Output: False2
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False8
Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8
Element Exists8
Element Exists9
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists1
Element Exists1
Element Exists7
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists44____1010
Element Exists22
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
9Element Exists
2Element Exists
3Checking if 15 exists in list
Yes, 15 exists in list
2Element Exists
5Check if an element exists in the list using any[] function
Python3
Does string contain any list element : True3
Input: 7 # Check if 7 exist or not. Output: False0
Does string contain any list element : True5
Input: 7 # Check if 7 exist or not. Output: False2
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False8
Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8
Element Exists8
Element Exists9
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists1
Element Exists1
Element Exists7
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists44____1010
Element Exists22
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 Exists
9Element Exists
2Element Exists
3Checking if 15 exists in list
Yes, 15 exists in list
2Element Exists
5count[]
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 Exists7
Element Exists3
Element Exists2
Element Exists9
lst
3Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False49
Element Exists5
Element Exists1
Element Exists2
Element Exists3
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists5
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 Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False63
Element Exists5
Element Exists6
Element Exists7
Element Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False70
Element Exists5
Output:
Checking if 15 exists in list Yes, 15 exists in list
Input: 7 # Check if 7 exist or not.
Output: False
14Input: 7 # Check if 7 exist or not.
Output: False
0 Input: 7 # Check if 7 exist or not.
Output: False
16Input: 7 # Check if 7 exist or not.
Output: False
17Element Exists
9 Does string contain any list element : True
3Checking if 15 exists in list
Yes, 15 exists in list
8
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False26
Input: 7 # Check if 7 exist or not. Output: False27
Input: 7 # Check if 7 exist or not. Output: False28
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False30
Input: 7 # Check if 7 exist or not. Output: False31
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 : True3
Input: 7 # Check if 7 exist or not. Output: False0
Does string contain any list element : True5
Input: 7 # Check if 7 exist or not. Output: False35
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False37
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False39
Input: 7 # Check if 7 exist or not. Output: False3__
Input: 7 # Check if 7 exist or not. Output: False51
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False53
Input: 7 # Check if 7 exist or not. Output: False37
Element Exists5
Các
Element Exists2
Element Exists3
Element Exists08
Element Exists5
Input: 7 # Check if 7 exist or not. Output: False76____10
Element Exists1212
Element Exists7
Element Exists2
Element Exists9
Element Exists17
Element Exists1
Element Exists2
Element Exists3
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists5
Element Exists2
Element Exists3
Element Exists25
Element Exists5
Element Exists27
Is
Element Exists1
Element Exists2
Element Exists3
Checking if 15 exists in list Yes, 15 exists in list2
Element Exists5
Element Exists6
Element Exists7
Element Exists1
Element Exists2
Element Exists3
Element Exists46
Element Exists5
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 Exists
2Element Exists
3Element Exists
25Element Exists
5
Python3
Is
Phương pháp 4: Sử dụng phương thức Find []
Element Exists67
Input: 7 # Check if 7 exist or not. Output: False0
Element Exists69
Element Exists3
Element Exists71
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False28
Element Exists74
Element Exists75
Input: 7 # Check if 7 exist or not. Output: False0
Element Exists77
Element Exists78
Does string contain any list element : True3
Input: 7 # Check if 7 exist or not. Output: False0
Does string contain any list element : True5
Input: 7 # Check if 7 exist or not. Output: False35
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False37
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False39
Input: 7 # Check if 7 exist or not. Output: False3__
Element Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False63
Element Exists5
Element Exists6
Element Exists7
Element Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False70
Element Exists5
Element Exists
2Element Exists
3Input: 7 # Check if 7 exist or not.
Output: False
49Element Exists
5
Checking if 15 exists in list Yes, 15 exists in list
Element Exists
7 Element Exists
80Element Exists
81Element Exists
82___
Đầ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 Exists7
Element Exists22
Input: 7 # Check if 7 exist or not. Output: False37
Element Exists24
Input: 7 # Check if 7 exist or not. Output: False58
Checking if 4 exists in list [ using set[] + in] : Element Exists Checking if 4 exists in list [ using sort[] + bisect_left[] ] : Element Exists8
Element Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False63
Element Exists5
Element Exists6
Element Exists7
Element Exists1
Element Exists2
Element Exists3
Input: 7 # Check if 7 exist or not. Output: False70
Element Exists5
Element Exists
2Element Exists
3Input: 7 # Check if 7 exist or not.
Output: False
49Element Exists
5
Yes, 15 exists in list