Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Cho một đối tượng, nhiệm vụ là kiểm tra xem đối tượng có phải là danh sách hay không.
Phương pháp số 1: Sử dụng isinstance
your object is a list your object is not a list9
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]2223
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]8
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']03031
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]3
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]5
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
python3 app.py Nope, 'Dark' Not found in the list0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']2
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]0
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
# app.py data_string = "The last season of Game of Thrones was not good" listA = ['Stranger Things', 'S Education', 'Game of Thrones'] print["The original string : " + data_string] print["The original list : " + str[listA]] res = [ele for ele in listA if[ele in data_string]] print["Does string contain any list element : " + str[bool[res]]]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]5
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
python3 app.py Nope, 'Dark' Not found in the list0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
Output:
your object is a list ! your object is not a list
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']5
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']6
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']7
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']8
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']9
Method #2: Using
# app.py data_string = "The last season of Game of Thrones was not good" listA = ['Stranger Things', 'S Education', 'Game of Thrones'] print["The original string : " + data_string] print["The original list : " + str[listA]] res = [ele for ele in listA if[ele in data_string]] print["Does string contain any list element : " + str[bool[res]]]9
Cho một đối tượng, nhiệm vụ là kiểm tra xem đối tượng có phải là danh sách hay không.
Phương pháp số 1: Sử dụng isinstance
your object is a list your object is not a list9
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]2223
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]8
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]3
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']03031
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
# app.py data_string = "The last season of Game of Thrones was not good" listA = ['Stranger Things', 'S Education', 'Game of Thrones'] print["The original string : " + data_string] print["The original list : " + str[listA]] res = [ele for ele in listA if[ele in data_string]] print["Does string contain any list element : " + str[bool[res]]]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]5
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
python3 app.py Nope, 'Dark' Not found in the list0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']2
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]0
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
# app.py data_string = "The last season of Game of Thrones was not good" listA = ['Stranger Things', 'S Education', 'Game of Thrones'] print["The original string : " + data_string] print["The original list : " + str[listA]] res = [ele for ele in listA if[ele in data_string]] print["Does string contain any list element : " + str[bool[res]]]0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]5
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]6
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]1
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]2
python3 app.py Nope, 'Dark' Not found in the list0
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]4
Output:
your object is a list your object is not a list
Xem mã sau.To check if the list contains the specific element or not in Python, use the “in” operator or “not in” operator. Let’s explore this topic in detail.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.check if the list contains an element in Python, use the “in” operator. The “in” operator checks if the list contains a specific item or not. It can also check if the element exists on the list or not using the list.count[] function.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.in operator” is the most convenient way to check if an item exists on the list or not.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.True if an item exists in the list and False if an item does not exist. The list need not be sorted to practice this approach of checking.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.True, and if not, then it returns False.
your object is a list
your object is not a list
7
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'S Eductation' in listA: print["Yes, 'S Eductation' found in List : ", listA]
Đầu ra
python3 app.py Yes, 'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']
Xem mã sau.
# app.py listA = ['Stranger Things', 'S Education', 'Game of Thrones'] if 'Dark' in listA: print["Yes, 'S Eductation' found in List : ", listA] else: print["Nope, 'Dark' not found in the list"]
Đầu ra
python3 app.py Nope, 'Dark' Not found in the list
your object is a list
your object is not a list
7False, and else block executes.
Bạn có thể thấy rằng danh sách đầu tiên chứa tất cả các yếu tố của danh sách thứ hai. Điều này là do chúng tôi đã kiểm tra danh sách đầu tiên bằng phương thức All [].
Sự kết luận
Từ đầu ra, Game of Thrones tồn tại trong danh sách.
# app.py data_string = "The last season of Game of Thrones was not good" listA = ['Stranger Things', 'S Education', 'Game of Thrones'] print["The original string : " + data_string] print["The original list : " + str[listA]] res = [ele for ele in listA if[ele in data_string]] print["Does string contain any list element : " + str[bool[res]]]
Đầu ra
Kiểm tra xem danh sách có chứa một mục sử dụng không trong toán tử nghịch đảo không.Python không phải là một toán tử sẵn có đánh giá đúng nếu nó không tìm thấy một biến trong chuỗi được chỉ định và false & nbsp; nếu không.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.zero or more for or if clauses. The expressions can be anything, meaning you can put all types of objects in lists.
& nbsp; python trong và không trong các nhà khai thác hoạt động tốt cho các danh sách, bộ dữ liệu, bộ và dicts [khóa kiểm tra].context of the for and if clauses follow it.
Trong ví dụ của chúng tôi, chúng tôi kiểm tra danh sách và cả với các mục chuỗi nếu chúng tôi có thể tìm thấy một trận đấu và trả về đúng.
Hãy để xem, chuỗi có chứa từ không tồn tại trong một mục của danh sách không.
________số 8Đầu ra
python3 app.py The original string: The last season of BoJack Horseman was good The original list : ['Stranger Things', 'S Education', 'Game of Thrones'] Does string contain any list element: False
Kiểm tra xem một phần tử có tồn tại trong danh sách bằng danh sách.count []
Để kiểm tra xem mục có tồn tại trong danh sách Python hay không, hãy sử dụng phương thức list.Count [].
Chức năng cú pháp của danh sách.count [] được theo sau.
your object is a list your object is not a list0
Phương thức Số lượng danh sách Python [Mục] Trả về số lượng xuất hiện của phần tử đã cho trong danh sách. Nếu nó lớn hơn 0, điều đó có nghĩa là một mục nhất định tồn tại trong danh sách. method returns the occurrence count of the given element in the list. If it’s greater than 0, it means a given item exists in the list.
your object is a list your object is not a list1
Đầu ra
your object is a list your object is not a list2
Kiểm tra xem một phần tử có tồn tại trong danh sách bằng bất kỳ []
Sử dụng python bất kỳ [] chức năng là cách cổ điển nhất mà bạn có thể thực hiện nhiệm vụ này và cũng hiệu quả. Hàm bất kỳ [] kiểm tra cho một trận đấu trong một chuỗi với khớp của mỗi phần tử danh sách.
your object is a list your object is not a list3
Đầu ra
your object is a list your object is not a list4
Từ đầu ra, Game of Thrones tồn tại trong danh sách.
Kiểm tra xem danh sách có chứa một mục sử dụng không trong toán tử nghịch đảo không.
Python không phải là một toán tử sẵn có đánh giá đúng nếu nó không tìm thấy một biến trong chuỗi được chỉ định và false & nbsp; nếu không.not in” is an inbuilt operator that evaluates to True if it does not finds a variable in the specified sequence and False otherwise.
Để kiểm tra xem danh sách có chứa một mục cụ thể hay không, bạn có thể sử dụng toán tử không trong toán tử nghịch đảo. Hãy cùng xem ví dụ sau đây.
your object is a list your object is not a list5
Đầu ra
your object is a list your object is not a list6
& nbsp; python trong và không trong các nhà khai thác hoạt động tốt cho các danh sách, bộ dữ liệu, bộ và dicts [khóa kiểm tra].
Phương thức Python All [] để kiểm tra xem danh sách có tồn tại trong một danh sách khác không
Trong chương trình này, bạn sẽ học cách kiểm tra xem danh sách Python có chứa tất cả các mục của danh sách khác và hiển thị kết quả bằng hàm python print [].
Chúng tôi sẽ sử dụng hai danh sách có giá trị chồng chéo. Một trong số đó là cái lớn chứa tất cả các mục của cái thứ hai.
- List1 - Dist1 chứa tất cả hoặc một số mục của danh sách khác.
- List2 - Đây là tập hợp con của cái đầu tiên.
Xem mã sau.
your object is a list your object is not a list7
Đầu ra
your object is a list your object is not a list8
Bạn có thể thấy rằng danh sách đầu tiên chứa tất cả các yếu tố của danh sách thứ hai. Điều này là do chúng tôi đã kiểm tra danh sách đầu tiên bằng phương thức All [].
Sự kết luận
Có nhiều cách tiếp cận bạn có thể sử dụng để xác định xem một mục có tồn tại trong danh sách hay không. Ví dụ, chúng tôi đã thấy những cách sau đây.
- Sử dụng python "trong nhà điều hành"
- Sử dụng danh sách Python hiểu biết
- Sử dụng phương thức list.count []
- Sử dụng hàm python bất kỳ []
Đó là nó cho hướng dẫn này.