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,Các nhà khai thác
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[1, 2] in [[3, 4], [1, 2]] # 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[3, 2, 1].__contains__[1] # True
9.[1, 2] in [[3, 4], [1, 2]] # True
Additionally,
Toán tử
6 được xác định là có giá trị thực nghịch đảo là[1, 2] in [[3, 4], [1, 2]] # True
5.[1, 2] in [[3, 4], [1, 2]] # True
[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....
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[1, 2] in [[3, 4], [1, 2]] # True
0. Ví dụ, bạn có thể [nhưng không nên] làm điều này: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, 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:
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.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]
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?
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
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 trên chúng không?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 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: True3
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True5
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
[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: True9
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
Input: 7 # Check if 7 exist or not. Output: False3
Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False5
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1______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 Exists2
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9 Element Exists4
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]
1l = 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]
2l = 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]
3l = 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]
22555555225Element Exists7
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
[1, 2] in [[3, 4], [1, 2]]
# True
5 [1, 2] in [[3, 4], [1, 2]]
# True
00Input: 7 # Check if 7 exist or not. Output: False0
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
[1, 2] in [[3, 4], [1, 2]]
# True
031 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
91 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: True1
[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 Exists2
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9 Element Exists4
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]
1l = 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]
2l = 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]
3l = 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]
22555555225Element Exists7
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
[1, 2] in [[3, 4], [1, 2]]
# True
5 [1, 2] in [[3, 4], [1, 2]]
# True
00Input: 7 # Check if 7 exist or not. Output: False0
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
[1, 2] in [[3, 4], [1, 2]]
# True
031 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
91 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: True1
[1, 2] in [[3, 4], [1, 2]]
# True
07Output:
Element Exists
‘Check if an element exists in the list using any[] function
Python3
Element Exists2
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9 Element Exists4
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]
1l = 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]
2l = 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]
3l = 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]
22555555225Element Exists7
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
[1, 2] in [[3, 4], [1, 2]]
# True
5 [1, 2] in [[3, 4], [1, 2]]
# True
00Input: 7 # Check if 7 exist or not. Output: False0
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
[1, 2] in [[3, 4], [1, 2]]
# True
031 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
91 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: True1
[1, 2] in [[3, 4], [1, 2]]
# True
07Output:
[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: True6
Input: 7 # Check if 7 exist or not. Output: False2
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True1
[1, 2] in [[3, 4], [1, 2]]
# True
5 [1, 2] in [[3, 4], [1, 2]]
# True
32Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[1, 2] in [[3, 4], [1, 2]]
# True
88Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[1, 2] in [[3, 4], [1, 2]]
# True
11Input: 7 # Check if 7 exist or not. Output: False4
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: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
02Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False5
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
09Input: 7 # Check if 7 exist or not. Output: False4
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
[1, 2] in [[3, 4], [1, 2]]
# True
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
[1, 2] in [[3, 4], [1, 2]]
# True
[1, 2] in [[3, 4], [1, 2]]
# True
[1, 2] in [[3, 4], [1, 2]]
# True
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[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__72Phươ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
151 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]
0l = 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]
1l = 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]
2l = 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]
3l = 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
301 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]
0l = 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]
1l = 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]
2l = 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]
3l = 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]
22555555225Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
47Input: 7 # Check if 7 exist or not. Output: False4
[3, 2, 1].__contains__[1]
# True
151 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
52list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True1
[1, 2] in [[3, 4], [1, 2]]
# True
5 [3, 2, 1].__contains__[1]
# True
56Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[1, 2] in [[3, 4], [1, 2]]
# True
11Input: 7 # Check if 7 exist or not. Output: False4
‘
[3, 2, 1].__contains__[1]
# True
66list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True6
[3, 2, 1].__contains__[1]
# True
68list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True1
[3, 2, 1].__contains__[1]
# True
701 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9__272727261Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[1, 2] in [[3, 4], [1, 2]]
# True
11Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False5
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
85Input: 7 # Check if 7 exist or not. Output: False4
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: True6
[3, 2, 1].__contains__[1]
# True
68list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True1
[3, 2, 1].__contains__[1]
# True
701 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9__272727261Phương pháp 4: Sử dụng phương thức Find []
[3, 2, 1].__contains__[1]
# True
11 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
91 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
4Input: 7 # Check if 7 exist or not. Output: False2
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]
10Input: 7 # Check if 7 exist or not. Output: False2
[1, 2] in [[3, 4], [1, 2]]
# True
67lst = 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]
13lst = 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]
141 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]
16lst = 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]
17Element Exists2
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9 Element Exists4
[1, 2] in [[3, 4], [1, 2]]
# True
74l = 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 ____176l = 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
78l = 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: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
02Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False5
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
09Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not.
Output: False
1Input: 7 # Check if 7 exist or not.
Output: False
2
88[1, 2] in [[3, 4], [1, 2]]
# True
Input: 7 # Check if 7 exist or not.
Output: False
4
[1, 2] in [[3, 4], [1, 2]]
# True
1list = 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]
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: False
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]
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]
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 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
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]
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]
Đầ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: True6
[3, 2, 1].__contains__[1]
# True
68list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True1
[3, 2, 1].__contains__[1]
# True
701 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
9__272727261Phươ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: 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]
61[1, 2] in [[3, 4], [1, 2]]
# True
76lst = 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
07Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
02Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not. Output: False5
Input: 7 # Check if 7 exist or not. Output: False6
Input: 7 # Check if 7 exist or not. Output: False0
Input: 7 # Check if 7 exist or not. Output: False1
Input: 7 # Check if 7 exist or not. Output: False2
[3, 2, 1].__contains__[1]
# True
09Input: 7 # Check if 7 exist or not. Output: False4
Input: 7 # Check if 7 exist or not.
Output: False
1Input: 7 # Check if 7 exist or not.
Output: False
2
88[1, 2] in [[3, 4], [1, 2]]
# True
Input: 7 # Check if 7 exist or not.
Output: False
4
[1, 2] in [[3, 4], [1, 2]]
# True
4