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
in
vànot in
Kiểm tra thành viên.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.[1, 2] in [[3, 4], [1, 2]] # True
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....
in
vànot 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áp3. Ví dụ, bạn có thể [nhưng không nên] làm điều này:[3, 2, 1].__contains__[1] # True
[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:
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.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 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?
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]
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
4 trên chúng khô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]
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
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
11 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
31 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
41 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
51 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
61 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
51 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
81 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
5l = 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]
01 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
5l = 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]
223your object is a list ! your object is not a list2
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
your object is a list ! your object is not a list5
your object is a list ! your object is not a list6
your object is a list ! your object is not a list7
your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
your object is a list your object is not a list2
your object is a list ! your object is not a list6
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]
8your object is a list your object is not a list6
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]
7your object is a list ! your object is not a list1
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
in
2your object is a list ! your object is not a list6
your object is a list ! your object is not a list7
your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
your object is a list your object is not a list2
your object is a list ! your object is not a list6
Output:
your object is a list ! your object is not a list
& nbsp; Phương pháp #2: Sử dụng not in
1
Method #2: Using not in
1
1 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
11 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
31 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
41 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
51 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
61 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
51 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
81 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
5l = 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]
01 in {1, 2, 3}
# True
'a' not in {'a', 'b', 'c'}
# False
[1, 2] in {['a', 'c'], [1, 2]}
# True
5l = 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]
223l = 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]
41 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 list4
[1, 2] in [[3, 4], [1, 2]]
# True
081 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
122l = 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]
7your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
in
2your object is a list ! your object is not a list6
your object is a list ! your object is not a list7
your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
your object is a list your object is not a list2
your object is a list ! your object is not a list6
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]
7your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
in
2your object is a list ! your object is not a list6
your object is a list ! your object is not a list7
your object is a list ! your object is not a list8
your object is a list ! your object is not a list9
your object is a list ! your object is not a list3
your object is a list ! your object is not a list4
your object is a list your object is not a list2
your object is a list ! your object is not a list6
Output:
your object is a list your object is not a list