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. Show
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 Python3Cá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 Python3Does 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” Python3Does 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
Python3Does 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 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; Python3Element 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: 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 list8Element 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
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 (). Python3Does 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 Exists2Element Exists3Element Exists25Element Exists5Python3Is 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
Checking if 15 exists in list Yes, 15 exists in list Element Exists7 Element Exists80Element Exists81Element Exists82___Đầu ra Python3Phươ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
Yes, 15 exists in list |