Hướng dẫn how do you check if something is not in a list python? - làm thế nào để bạn kiểm tra nếu một cái gì đó không có trong một danh sách python?

Làm cách nào để kiểm tra xem một cái gì đó là (không) trong danh sách trong Python?

Giải pháp rẻ nhất và dễ đọc nhất là sử dụng toán tử in (hoặc trong trường hợp cụ thể của bạn, not in). Như đã đề cập trong tài liệu,

Các nhà khai thác innot in Kiểm tra thành viên.

(1, 2) in [(3, 4), (1, 2)]
#  True
2 đánh giá thành
(1, 2) in [(3, 4), (1, 2)]
#  True
3 nếu
(1, 2) in [(3, 4), (1, 2)]
#  True
4 là thành viên của
(1, 2) in [(3, 4), (1, 2)]
#  True
5 và
(1, 2) in [(3, 4), (1, 2)]
#  True
6 nếu không.
(1, 2) in [(3, 4), (1, 2)]
#  True
7 Trả về sự phủ định của
(1, 2) in [(3, 4), (1, 2)]
#  True
2.

Additionally,

Toán tử not in được xác định là có giá trị thực nghịch là in.

[3, 2, 1].__contains__(1)
# True
1 về mặt logic giống như
[3, 2, 1].__contains__(1)
# True
2.

Đây là vài ví dụ:

'a' in [1, 2, 3]
# False

'c' in ['a', 'b', 'c']
# True

'a' not in [1, 2, 3]
# True

'c' not in ['a', 'b', 'c']
# False

Điều này cũng hoạt động với các bộ dữ liệu, vì các bộ dữ liệu có thể băm (do hậu quả của thực tế là chúng cũng là bất biến):

(1, 2) in [(3, 4), (1, 2)]
#  True

Nếu đối tượng trên RHS xác định phương thức

[3, 2, 1].__contains__(1)
# True
3, in sẽ gọi nó trong nội bộ, như đã lưu ý trong đoạn cuối của phần so sánh của tài liệu.

... innot in, được hỗ trợ bởi các loại có thể sử dụng được hoặc thực hiện phương pháp

[3, 2, 1].__contains__(1)
# True
3. Ví dụ, bạn có thể (nhưng không nên) làm điều này:

[3, 2, 1].__contains__(1)
# True

in ngắn mạch, vì vậy nếu phần tử của bạn ở đầu danh sách, in sẽ đánh giá nhanh hơn:

lst = list(range(10001))
%timeit 1 in lst
%timeit 10000 in lst  # Expected to take longer time.

68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Nếu bạn muốn làm nhiều hơn là chỉ kiểm tra xem một mục có nằm trong danh sách hay không, có các tùy chọn:

  • lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    0 có thể được sử dụng để lấy chỉ mục của một mục. Nếu yếu tố đó không tồn tại,
    lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    1 sẽ được nâng lên.
  • lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    2 có thể được sử dụng nếu bạn muốn đếm các lần xuất hiện.

Vấn đề XY: Bạn đã xem xét lst = list(range(10001)) %timeit 1 in lst %timeit 10000 in lst # Expected to take longer time. 68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) 178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) 3S chưa?

Hãy tự hỏi mình những câu hỏi sau:

  • Bạn có cần kiểm tra xem một mục có nằm trong danh sách nhiều lần không một lần?
  • Là kiểm tra này được thực hiện bên trong một vòng lặp, hoặc một chức năng được gọi là lặp đi lặp lại?
  • Các mặt hàng bạn đang lưu trữ trong danh sách của bạn có thể có thể không? Iow, bạn có thể gọi
    lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    4 trên chúng không?

Nếu bạn trả lời "có" cho những câu hỏi này, bạn nên sử dụng

lst = list(range(10001))
%timeit 1 in lst
%timeit 10000 in lst  # Expected to take longer time.

68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
3 thay thế. Một bài kiểm tra thành viên in trên
lst = list(range(10001))
%timeit 1 in lst
%timeit 10000 in lst  # Expected to take longer time.

68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
7S là độ phức tạp thời gian O (N). Điều này có nghĩa là Python phải thực hiện quét tuyến tính danh sách của bạn, truy cập từng yếu tố và so sánh nó với mục tìm kiếm. Nếu bạn đang làm điều này nhiều lần hoặc nếu danh sách lớn, hoạt động này sẽ phải chịu một chi phí.

Mặt khác, các đối tượng ____33, băm giá trị của chúng để kiểm tra thành viên thời gian liên tục. Kiểm tra cũng được thực hiện bằng cách sử dụng in:

1 in {1, 2, 3} 
# True

'a' not in {'a', 'b', 'c'}
# False

(1, 2) in {('a', 'c'), (1, 2)}
# True

Nếu bạn không may là yếu tố bạn đang tìm kiếm/không tìm kiếm ở cuối danh sách của bạn, Python sẽ quét danh sách cho đến cuối. Điều này thể hiện rõ từ thời gian dưới đây:

l = list(range(100001))
s = set(l)

%timeit 100000 in l
%timeit 100000 in s

2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Một lời nhắc nhở, đây là một tùy chọn phù hợp miễn là các yếu tố bạn lưu trữ và tìm kiếm có thể băm. IOW, họ sẽ phải là loại bất biến hoặc các đối tượng thực hiện

1 in {1, 2, 3} 
# True

'a' not in {'a', 'b', 'c'}
# False

(1, 2) in {('a', 'c'), (1, 2)}
# True
0.

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

    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    1
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    2
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    3
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    4
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    6
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    8
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    0
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    223

    your object is a list !
    your object is not a list
    
    2
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4
    your object is a list !
    your object is not a list
    
    5
    your object is a list !
    your object is not a list
    
    6

    your object is a list !
    your object is not a list
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4
    your object is a list
    your object is not a list
    
    2
    your object is a list !
    your object is not a list
    
    6

    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    7
    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    8
    your object is a list
    your object is not a list
    
    6
    lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    7
    your object is a list !
    your object is not a list
    
    1

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4in2
    your object is a list !
    your object is not a list
    
    6

    your object is a list !
    your object is not a list
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4
    your object is a list
    your object is not a list
    
    2
    your object is a list !
    your object is not a list
    
    6

    Output:

    your object is a list !
    your object is not a list
    

    & nbsp; Phương pháp #2: Sử dụng not in1
    Method #2: Using not in1

    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    1
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    2
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    3
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    4
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    6
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    8
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    0
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    223

    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    4
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    2
    your object is a list !
    your object is not a list
    
    4
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    08
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    5
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    10____45
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    122

    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    7
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    15
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    16
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    17
    lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4in2
    your object is a list !
    your object is not a list
    
    6

    your object is a list !
    your object is not a list
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4
    your object is a list
    your object is not a list
    
    2
    your object is a list !
    your object is not a list
    
    6

    l = list(range(100001))
    s = set(l)
    
    %timeit 100000 in l
    %timeit 100000 in s
    
    2.58 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    101 ns ± 9.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    7
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    15
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    34
    (1, 2) in [(3, 4), (1, 2)]
    #  True
    
    17
    lst = list(range(10001))
    %timeit 1 in lst
    %timeit 10000 in lst  # Expected to take longer time.
    
    68.9 ns ± 0.613 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    178 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4in2
    your object is a list !
    your object is not a list
    
    6

    your object is a list !
    your object is not a list
    
    7
    your object is a list !
    your object is not a list
    
    8

    your object is a list !
    your object is not a list
    
    9
    your object is a list !
    your object is not a list
    
    3
    your object is a list !
    your object is not a list
    
    4
    your object is a list
    your object is not a list
    
    2
    your object is a list !
    your object is not a list
    
    6

    Output:

    your object is a list
    your object is not a list
    


    Làm thế nào để bạn kiểm tra xem một mục không có trong danh sách Python?

    Không phải trong toán tử trong nhà điều hành - toán tử này được sử dụng để kiểm tra xem một phần tử không có trong danh sách được truyền hay không.Trả về true nếu phần tử không có trong danh sách khác trả về sai. − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false.

    Làm thế nào để bạn kiểm tra xem một đối tượng có nằm trong danh sách trong Python không?

    Sử dụng hàm bất kỳ () để kiểm tra xem một đối tượng có tồn tại trong danh sách các đối tượng không.Hàm bất kỳ () sẽ trả về true nếu đối tượng tồn tại trong danh sách, nếu không thì sai được trả về. to check if an object exists in a list of objects. The any() function will return True if the object exists in the list, otherwise False is returned.

    Làm thế nào để bạn kiểm tra xem một phần tử có tồn tại trong danh sách các danh sách Python không?

    Phương thức số 1: Sử dụng bất kỳ () bất kỳ () nào trả về true bất cứ khi nào một phần tử cụ thể có mặt trong một trình lặp nhất định.Using any() any() method return true whenever a particular element is present in a given iterator.