Tôi biết đây là một câu hỏi cũ, nhưng tôi muốn thêm một câu trả lời mà tôi tin là tốt hơn.
Nếu tất cả các yếu tố phải được kiểm tra có thể băm, bạn có thể sử dụng một bộ thay vì danh sách hoặc tuple.
>>> None not in {1, 84, 'String', [6, 'Tuple'], 3}
Điều này nhanh hơn nhiều so với các phương pháp trong các câu trả lời khác.
$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
Một lợi thế khác của phương pháp này là nó cung cấp cho bạn câu trả lời chính xác ngay cả khi ai đó định nghĩa phương thức __eq__
của một lớp để luôn trả về True
. .
class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
Kiểm tra xem nhiều biến không có trong Python #
Để kiểm tra xem nhiều biến không phải là không:
- Bọc các biến trong một danh sách.
- Sử dụng một biểu thức máy phát để lặp qua danh sách.
- Trên mỗi lần lặp, kiểm tra xem mục danh sách hiện tại không phải là
2.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
- Chuyển đối tượng Trình tạo đến hàm
3.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
Copied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
Chúng tôi đã sử dụng dấu ngoặc vuông để thêm các biến vào danh sách và sử dụng biểu thức trình tạo vào lặp qua danh sách.
Biểu thức của máy phát được sử dụng để thực hiện một số hoạt động cho mọi phần tử hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.
Trên mỗi lần lặp, chúng tôi kiểm tra xem mục danh sách hiện tại không phải là
$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 và trả về kết quả.Bước cuối cùng là chuyển đối tượng
$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
5 cho hàm $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
3.Chức năng tích hợp tất cả [] lấy một sự khác biệt như một đối số và trả về True
nếu tất cả các yếu tố của điều đó là sự thật [hoặc điều đó là trống rỗng].
Nếu tất cả các biến không phải là
2, hàm $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
3 sẽ trả về $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
True
, nếu không
1 được trả về.class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
Một cách tiếp cận khác là sử dụng toán tử
class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
2.Để kiểm tra xem nhiều biến không phải là không:
- Bọc các biến trong một chuỗi [ví dụ: một tuple hoặc một danh sách].
- Sử dụng toán tử
3 để kiểm tra xemclass Int[int]: def __eq__[self, other]: return True def __hash__[self]: return hash[super[]] print[all[v is not None for v in [1, Int[6], 2]]] # True [correct] print[None not in [1, Int[6], 2]] # False [wrong] print[None not in [1, Int[6], 2]] # False [wrong] print[None not in {1, Int[6], 2}] # True [correct]
2 không phải là thành viên của chuỗi.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
- Nếu chuỗi không chứa
2, thì các biến không phải là$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
2.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
Copied!
a = 'a' b = 'b' c = 'c' if None not in [a, b, c]: # 👇️ this runs print['Multiple variables are NOT None']
Cách tiếp cận này trông đơn giản hơn nhiều so với phương pháp trước. Tuy nhiên, các toán tử
class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
2 và class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
3 kiểm tra sự bình đẳng, ví dụ: class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
9, 0, v.v.Copied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
Đây không phải là một thông lệ tốt trong Python, vì nên kiểm tra
$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 bằng cách sử dụng từ khóa 2.Copied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
Có một số trường hợp rất hiếm hoi khi sử dụng bằng và không bằng để kiểm tra Copied! Copied!
2 có thể dẫn đến hành vi khó hiểu, do đó, Hướng dẫn kiểu PEP8 khuyên bạn nên sử dụng $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2 và a = 'a'
b = 'b'
c = 'c'
if all[item is not None for item in [a, b, c]]:
print['Multiple variables are NOT None']
else:
print['At least one of the variables stores a None value']
5 khi kiểm tra a = 'a'
b = 'b'
c = 'c'
if all[item is not None for item in [a, b, c]]:
print['Multiple variables are NOT None']
else:
print['At least one of the variables stores a None value']
2.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,
7 đánh giá thànhCopied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
True
nếu 9 là thành viên củaCopied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
0, nếu không nó sẽ đánh giá thànhCopied!
a = 'a' b = 'b' c = 'c' if None not in [a, b, c]: # 👇️ this runs print['Multiple variables are NOT None']
class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
1.2 trả về sự phủ định củaCopied!
a = 'a' b = 'b' c = 'c' if None not in [a, b, c]: # 👇️ this runs print['Multiple variables are NOT None']
7.Copied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']
Một cách tiếp cận khác là sử dụng toán tử
4 nhiều lần.Copied!
a = 'a' b = 'b' c = 'c' if None not in [a, b, c]: # 👇️ this runs print['Multiple variables are NOT None']
Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
Điều này thường không được khuyến khích, vì nó khá lặp đi lặp lại và khó đọc.
Câu lệnh Copied! Copied! Copied!
5 đầu tiên kiểm tra xem biến a = 'a'
b = 'b'
c = 'c'
if None not in [a, b, c]:
# 👇️ this runs
print['Multiple variables are NOT None']
6 không phải là a = 'a'
b = 'b'
c = 'c'
if None not in [a, b, c]:
# 👇️ this runs
print['Multiple variables are NOT None']
2. Nếu nó, các đoạn ngắn mạch $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
5 trả về a = 'a'
b = 'b'
c = 'c'
if None not in [a, b, c]:
# 👇️ this runs
print['Multiple variables are NOT None']
1 mà không kiểm tra bất kỳ điều kiện nào sau đây.class Int[int]:
def __eq__[self, other]:
return True
def __hash__[self]:
return hash[super[]]
print[all[v is not None for v in [1, Int[6], 2]]] # True [correct]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in [1, Int[6], 2]] # False [wrong]
print[None not in {1, Int[6], 2}] # True [correct]
Biểu thức
0 trả về giá trị bên trái nếu nó giả mạo, nếu không giá trị bên phải được trả về.Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
Copied!
result = False and 'hello' print[result] # 👉️ False
Tất cả các giá trị không phải là sự thật được coi là giả mạo. Các giá trị giả trong Python là:
- Các hằng số được xác định là giả mạo:
2 và$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]" 200000 loops, best of 5: 999 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop $ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]" 2000000 loops, best of 5: 184 nsec per loop python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}" 5000000 loops, best of 5: 48.6 nsec per loop
1.class Int[int]: def __eq__[self, other]: return True def __hash__[self]: return hash[super[]] print[all[v is not None for v in [1, Int[6], 2]]] # True [correct] print[None not in [1, Int[6], 2]] # False [wrong] print[None not in [1, Int[6], 2]] # False [wrong] print[None not in {1, Int[6], 2}] # True [correct]
3 [không] thuộc bất kỳ loại số nàoCopied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
- Trình tự và bộ sưu tập trống:
4 [Chuỗi trống],Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
5 [Tuple trống],Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
6 [danh sách trống],Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
7 [Từ điển trống],Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
8 [bộ trống],Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
9 [phạm vi trống].Copied!
a = 'a' b = 'b' c = 'c' if a is not None and b is not None and c is not None: print['Multiple variables are NOT None']
Vì vậy, nếu giá trị bên trái là bất kỳ giá trị giả nào đã nói ở trên, giá trị bên trái sẽ được trả về.
Tốt nhất là sử dụng hàm
3 để kiểm tra xem nhiều biến không phải là $ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
2, vì phương pháp này không có bất kỳ trường hợp cạnh nào và khá dễ đọc.$ python3 -m timeit "all[v is not None for v in [1, 84, 'String', [6, 'Tuple'], 3]]"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', [6, 'Tuple'], 3]"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', [6, 'Tuple'], 3}"
5000000 loops, best of 5: 48.6 nsec per loop
Copied!
a = 'a' b = 'b' c = 'c' if all[item is not None for item in [a, b, c]]: print['Multiple variables are NOT None'] else: print['At least one of the variables stores a None value']