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: TrueInput: 7 # Check if 7 exist or not. Output: FalsePhươ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 Exists4Input: 7 # Check if 7 exist or not. Output: False0Element Exists6
Element Exists7 Element Exists8Element Exists9 Element Exists0
Element Exists1Element Exists2Element Exists3Element Exists4Element Exists5
Element Exists6Element Exists7
Element Exists1Element Exists2Element Exists3Does string contain any list element : True1Element 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 : True3Input: 7 # Check if 7 exist or not. Output: False0 Does string contain any list element : True5Input: 7 # Check if 7 exist or not. Output: False2Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False4Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False6Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False8Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8 Element Exists8Element 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 Exists1Element Exists7Checking 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 Exists9Element Exists2Element Exists3Checking if 15 exists in list Yes, 15 exists in list2Element Exists5
Output:
Element ExistsVí 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 : True3Input: 7 # Check if 7 exist or not. Output: False0 Does string contain any list element : True5Input: 7 # Check if 7 exist or not. Output: False2Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False4Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False6Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False8Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8 Element Exists8Element 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 Exists1Element Exists7Checking 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 ExistsChecking 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 Exists5Check if an element exists in the list using any() function
Python3
Does string contain any list element : True3Input: 7 # Check if 7 exist or not. Output: False0 Does string contain any list element : True5Input: 7 # Check if 7 exist or not. Output: False2Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False4Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False6Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False8Input: 7 # Check if 7 exist or not. Output: False3__
Checking if 15 exists in list Yes, 15 exists in list8 Element Exists8Element 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 Exists1Element Exists7Checking 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 : TrueChecking 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 Exists7 Element Exists3Element Exists2 Element Exists9 lst3
Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False49Element Exists5
Element Exists1Element Exists2Element Exists3Checking if 15 exists in list Yes, 15 exists in list2Element 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 Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False63Element Exists5
Element Exists6Element Exists7
Element Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False70Element Exists5
Output:
Checking if 15 exists in list Yes, 15 exists in listInput: 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 Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False26 Input: 7 # Check if 7 exist or not. Output: False27Input: 7 # Check if 7 exist or not. Output: False28Element Exists3Input: 7 # Check if 7 exist or not. Output: False30Input: 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 : True3Input: 7 # Check if 7 exist or not. Output: False0 Does string contain any list element : True5Input: 7 # Check if 7 exist or not. Output: False35Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False37Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False39Input: 7 # Check if 7 exist or not. Output: False3__
Input: 7 # Check if 7 exist or not. Output: False51Input: 7 # Check if 7 exist or not. Output: False0 Input: 7 # Check if 7 exist or not. Output: False53Input: 7 # Check if 7 exist or not. Output: False37Element Exists5
Các
Element Exists2Element Exists3Element Exists08Element Exists5
Input: 7 # Check if 7 exist or not. Output: False76____10 Element Exists1212
Element Exists7 Element Exists2 Element Exists9 Element Exists17
Element Exists1Element Exists2 Element Exists3Checking if 15 exists in list Yes, 15 exists in list2Element Exists5
Element Exists2Element Exists3Element Exists25Element Exists5
Element Exists27
Is
Element Exists1Element Exists2 Element Exists3Checking if 15 exists in list Yes, 15 exists in list2Element Exists5
Element Exists6Element Exists7
Element Exists1Element Exists2Element Exists3Element Exists46Element Exists5
Output:
Checking if 4 exists in list ( using set() + in) : Element Exists Checking if 4 exists in list ( using sort() + bisect_left() ) : Element ExistsElement Exists2Element Exists3Element Exists25Element Exists5
Python3
Is
Phương pháp 4: Sử dụng phương thức Find ()
Element Exists67Input: 7 # Check if 7 exist or not. Output: False0Element Exists69Element Exists3Element Exists71Element Exists3Input: 7 # Check if 7 exist or not. Output: False28Element Exists74
Element Exists75Input: 7 # Check if 7 exist or not. Output: False0Element Exists77Element Exists78
Does string contain any list element : True3Input: 7 # Check if 7 exist or not. Output: False0 Does string contain any list element : True5Input: 7 # Check if 7 exist or not. Output: False35Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False37Input: 7 # Check if 7 exist or not. Output: False3Input: 7 # Check if 7 exist or not. Output: False39Input: 7 # Check if 7 exist or not. Output: False3__
Element Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False63Element Exists5
Element Exists6Element Exists7
Element Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False70Element Exists5
Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False49Element Exists5
Checking if 15 exists in list Yes, 15 exists in listElement 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 Exists7Element Exists22Input: 7 # Check if 7 exist or not. Output: False37Element Exists24Input: 7 # Check if 7 exist or not. Output: False58Checking if 4 exists in list ( using set() + in) : Element Exists Checking if 4 exists in list ( using sort() + bisect_left() ) : Element Exists8
Element Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False63Element Exists5
Element Exists6Element Exists7
Element Exists1Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False70Element Exists5
Element Exists2Element Exists3Input: 7 # Check if 7 exist or not. Output: False49Element Exists5
Yes, 15 exists in list