Hướng dẫn how do you check if an element is not in a list in python? - làm thế nào để bạn kiểm tra xem một phần tử không có trong danh sách trong 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ử

(1, 2) in [(3, 4), (1, 2)]
#  True
5 (hoặc trong trường hợp cụ thể của bạn,
(1, 2) in [(3, 4), (1, 2)]
#  True
6). Như đã đề cập trong tài liệu,

Show

Các nhà khai thác

(1, 2) in [(3, 4), (1, 2)]
#  True
5 và
(1, 2) in [(3, 4), (1, 2)]
#  True
6 Kiểm tra thành viên.
(1, 2) in [(3, 4), (1, 2)]
#  True
9 đánh giá thành
[3, 2, 1].__contains__(1)
# True
0 nếu
[3, 2, 1].__contains__(1)
# True
1 là thành viên của
[3, 2, 1].__contains__(1)
# True
2 và
[3, 2, 1].__contains__(1)
# True
3 khác.
[3, 2, 1].__contains__(1)
# True
4 trả về sự phủ định của
(1, 2) in [(3, 4), (1, 2)]
#  True
9.

Additionally,

Toán tử

(1, 2) in [(3, 4), (1, 2)]
#  True
6 được xác định là có giá trị thực nghịch đảo là
(1, 2) in [(3, 4), (1, 2)]
#  True
5.

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

Đâ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

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,
(1, 2) in [(3, 4), (1, 2)]
#  True
5 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.

...

(1, 2) in [(3, 4), (1, 2)]
#  True
5 và
(1, 2) in [(3, 4), (1, 2)]
#  True
6, đượ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
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. Ví dụ, bạn có thể (nhưng không nên) làm điều này:

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

(1, 2) in [(3, 4), (1, 2)]
#  True
5 ngắn mạch, vì vậy nếu phần tử của bạn ở đầu danh sách,
(1, 2) in [(3, 4), (1, 2)]
#  True
5 đá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)
    
    7 có thể được sử dụng để truy xuất 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)
    
    8 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)
    
    9 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 1 in {1, 2, 3} # True 'a' not in {'a', 'b', 'c'} # False (1, 2) in {('a', 'c'), (1, 2)} # True 0s 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
    1 in {1, 2, 3} 
    # True
    
    'a' not in {'a', 'b', 'c'}
    # False
    
    (1, 2) in {('a', 'c'), (1, 2)}
    # True
    
    1 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

1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
0 thay thế. Một bài kiểm tra thành viên
(1, 2) in [(3, 4), (1, 2)]
#  True
5 trên
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
4S 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

1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
0 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
(1, 2) in [(3, 4), (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

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

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: 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

Python3

Các

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
3
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
5

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7
(1, 2) in [(3, 4), (1, 2)]
#  True
5
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
9

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
Input: 7  # Check if 7 exist or not.
Output: False
3
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
5
Input: 7  # Check if 7 exist or not.
Output: False
6

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1______72

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

Element Exists
2
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
Element Exists
4
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)
1
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)
2
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)
3
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)
22555555225

Element Exists
7
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7
(1, 2) in [(3, 4), (1, 2)]
#  True
5
(1, 2) in [(3, 4), (1, 2)]
#  True
00

Input: 7  # Check if 7 exist or not.
Output: False
0
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
(1, 2) in [(3, 4), (1, 2)]
#  True
03
1 in {1, 2, 3} 
# True

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

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

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
(1, 2) in [(3, 4), (1, 2)]
#  True
07

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” 

Python3

Element Exists
2
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
Element Exists
4
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)
1
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)
2
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)
3
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)
22555555225

Element Exists
7
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7
(1, 2) in [(3, 4), (1, 2)]
#  True
5
(1, 2) in [(3, 4), (1, 2)]
#  True
00

Input: 7  # Check if 7 exist or not.
Output: False
0
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
(1, 2) in [(3, 4), (1, 2)]
#  True
03
1 in {1, 2, 3} 
# True

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

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

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
(1, 2) in [(3, 4), (1, 2)]
#  True
07

Output:

Element Exists

Check if an element exists in the list using any() function

Python3

Element Exists
2
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
Element Exists
4
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)
1
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)
2
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)
3
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)
22555555225

Element Exists
7
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
7
(1, 2) in [(3, 4), (1, 2)]
#  True
5
(1, 2) in [(3, 4), (1, 2)]
#  True
00

Input: 7  # Check if 7 exist or not.
Output: False
0
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
(1, 2) in [(3, 4), (1, 2)]
#  True
03
1 in {1, 2, 3} 
# True

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

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

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
(1, 2) in [(3, 4), (1, 2)]
#  True
07

Output:

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

count()

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

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
Input: 7  # Check if 7 exist or not.
Output: False
2
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
(1, 2) in [(3, 4), (1, 2)]
#  True
5
(1, 2) in [(3, 4), (1, 2)]
#  True
32

Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
88
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
11
Input: 7  # Check if 7 exist or not.
Output: False
4

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

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
02
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
5
Input: 7  # Check if 7 exist or not.
Output: False
6

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
09
Input: 7  # Check if 7 exist or not.
Output: False
4

Output:

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

(1, 2) in [(3, 4), (1, 2)] # True 531 in {1, 2, 3} # True 'a' not in {'a', 'b', 'c'} # False (1, 2) in {('a', 'c'), (1, 2)} # True 9 (1, 2) in [(3, 4), (1, 2)] # True 55(1, 2) in [(3, 4), (1, 2)] # True 56(1, 2) in [(3, 4), (1, 2)] # True 5

Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
65
(1, 2) in [(3, 4), (1, 2)]
#  True
66
(1, 2) in [(3, 4), (1, 2)]
#  True
67__72

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.

Thể hiện để kiểm tra sự tồn tại của phần tử trong danh sách bằng set () + in và sort () + bisect_left ()

Python3

[3, 2, 1].__contains__(1)
# True
11
[3, 2, 1].__contains__(1)
# True
12
[3, 2, 1].__contains__(1)
# True
13
[3, 2, 1].__contains__(1)
# True
14

[3, 2, 1].__contains__(1)
# True
15
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
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
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)
1
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)
2
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)
3
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)
225555.

[3, 2, 1].__contains__(1)
# True
30
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
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
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)
1
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)
2
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)
3
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)
22555555225

Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
47
Input: 7  # Check if 7 exist or not.
Output: False
4

[3, 2, 1].__contains__(1)
# True
15
1 in {1, 2, 3} 
# True

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

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

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
0
[3, 2, 1].__contains__(1)
# True
52

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
(1, 2) in [(3, 4), (1, 2)]
#  True
5
[3, 2, 1].__contains__(1)
# True
56

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
11
Input: 7  # Check if 7 exist or not.
Output: False
4

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

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
[3, 2, 1].__contains__(1)
# True
68
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
[3, 2, 1].__contains__(1)
# True
70
1 in {1, 2, 3} 
# True

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

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

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
11
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
5
Input: 7  # Check if 7 exist or not.
Output: False
6

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
85
Input: 7  # Check if 7 exist or not.
Output: False
4

Output:

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

Python3

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
[3, 2, 1].__contains__(1)
# True
68
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
[3, 2, 1].__contains__(1)
# True
70
1 in {1, 2, 3} 
# True

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

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

Phương pháp 4: Sử dụng phương thức Find ()

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

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

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

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
4
Input: 7  # Check if 7 exist or not.
Output: False
2
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)
10
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
67
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)
13

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)
14
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
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)
16
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)
17

Element Exists
2
1 in {1, 2, 3} 
# True

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

(1, 2) in {('a', 'c'), (1, 2)}
# True
9
Element Exists
4
(1, 2) in [(3, 4), (1, 2)]
#  True
74
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)
22 ____176
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)
2
(1, 2) in [(3, 4), (1, 2)]
#  True
78
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)
22.

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
02
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
5
Input: 7  # Check if 7 exist or not.
Output: False
6

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
09
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
88
Input: 7  # Check if 7 exist or not.
Output: False
4

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

list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6 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) 19lst = 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) 20lst = 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) 211 in {1, 2, 3} # True 'a' not in {'a', 'b', 'c'} # False (1, 2) in {('a', 'c'), (1, 2)} # True 9lst = 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) 23l = 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) 1Input: 7 # Check if 7 exist or not. Output: False6

Đầu ra

Python3

Phương pháp 5: Sử dụng hàm bộ đếm ()

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
6
[3, 2, 1].__contains__(1)
# True
68
list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
1
[3, 2, 1].__contains__(1)
# True
70
1 in {1, 2, 3} 
# True

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

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

Phương pháp 4: Sử dụng phương thức Find ()

list = test_list = [1, 6, 3, 5, 3, 4]
Input: 3  # Check if 3 exist or not.
Output: True
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)
61
(1, 2) in [(3, 4), (1, 2)]
#  True
76
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)
63
(1, 2) in [(3, 4), (1, 2)]
#  True
97
(1, 2) in [(3, 4), (1, 2)]
#  True
07

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
02
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
5
Input: 7  # Check if 7 exist or not.
Output: False
6

Input: 7  # Check if 7 exist or not.
Output: False
0
Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
[3, 2, 1].__contains__(1)
# True
09
Input: 7  # Check if 7 exist or not.
Output: False
4

Input: 7  # Check if 7 exist or not.
Output: False
1
Input: 7  # Check if 7 exist or not.
Output: False
2
(1, 2) in [(3, 4), (1, 2)]
#  True
88
Input: 7  # Check if 7 exist or not.
Output: False
4

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


Làm thế nào để bạn tìm thấy các yếu tố không có trong danh sách?

Để tìm các phần tử trong một danh sách không có trong phạm vi khác: sử dụng lớp SET () để chuyển đổi danh sách đầu tiên thành một đối tượng đã đặt. Sử dụng phương thức chênh lệch () để có các phần tử trong tập hợp không có trong danh sách.Use the set() class to convert the first list to a set object. Use the difference() method to get the elements in the set that are not in the list.

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.

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.