Hướng dẫn how do i check a list in python? - làm cách nào để kiểm tra danh sách trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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 list
    
    9
    # 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 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")
    
    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 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")
    
    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 list
    
    9
    # 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 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")
    
    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 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")
    
    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 list
    
    0

    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 list
    
    1

    Đầu ra

    your object is a list
    your object is not a list
    
    2

    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 list
    
    3

    Đầu ra

    your object is a list
    your object is not a list
    
    4

    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 list
    
    5

    Đầu ra

    your object is a list
    your object is not a list
    
    6

    & 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.

    1. List1 - Dist1 chứa tất cả hoặc một số mục của danh sách khác.
    2. 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 list
    
    7

    Đầu ra

    your object is a list
    your object is not a list
    
    8

    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.

    1. Sử dụng python "trong nhà điều hành"
    2. Sử dụng danh sách Python hiểu biết
    3. Sử dụng phương thức list.count ()
    4. Sử dụng hàm python bất kỳ ()

    Đó là nó cho hướng dẫn này.

    Làm thế nào để bạn kiểm tra xem đó là danh sách trong Python?

    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.if isInstance (ini_list1, list): in ("đối tượng của bạn là một danh sách!")if isinstance (ini_list1, list ): print ( "your object is a list !" )

    Làm thế nào để bạn kiểm tra tất cả các mục trong danh sách Python?

    Python tất cả () hàm chức năng tất cả () trả về đúng nếu tất cả các mục trong một điều có thể là đúng, nếu không nó sẽ trả về sai.Nếu đối tượng có thể xóa, hàm tất cả () cũng trả về true.all() Function The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.

    Làm thế nào để bạn kiểm tra xem một phần tử có danh sách?

    Kiểm tra xem phần tử có tồn tại trong danh sách bằng cách sử dụng hàm danh sách.Count (phần tử) trả về số lượng xuất hiện của phần tử đã cho trong danh sách không.Nếu nó lớn hơn 0, nó có nghĩa là phần tử được đưa ra trong danh sách.list. count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

    Làm cách nào để kiểm tra tất cả các giá trị trong một danh sách?

    Sử dụng tất cả () hàm chúng ta có thể kiểm tra xem tất cả các giá trị có lớn hơn bất kỳ giá trị nào trong một dòng không.Nó trả về đúng nếu điều kiện đã cho bên trong hàm tất cả () là đúng đối với tất cả các giá trị, nếu không nó sẽ trả về sai. we can check if all values are greater than any given value in a single line. It returns true if the given condition inside the all() function is true for all values, else it returns false.