Hướng dẫn python reverse boolean - boolean đảo ngược python

Toán tử if bool == True: return False else: return True 5 (phủ định logic)

Có lẽ cách tốt nhất là sử dụng toán tử if bool == True: return False else: return True 5:

>>> value = True >>> not value False >>> value = False >>> not value True

Vì vậy, thay vì mã của bạn:

if bool == True: return False else: return True

Bạn đã có thể sử dụng:

return not bool

Phủ định logic là chức năng

Ngoài ra còn có hai chức năng trong mô -đun if bool == True: return False else: return True 7 if bool == True: return False else: return True 8 và đó là bí danh if bool == True: return False else: return True 9 trong trường hợp bạn cần nó là chức năng thay vì là toán tử:

>>> import operator >>> operator.not_(False) True >>> operator.not_(True) False

Chúng có thể hữu ích nếu bạn muốn sử dụng một chức năng yêu cầu chức năng vị ngữ hoặc gọi lại.

Ví dụ return not bool 0 hoặc return not bool 1:

>>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False]

Tất nhiên điều tương tự cũng có thể đạt được với hàm return not bool 2 tương đương:

>>> my_not_function = lambda item: not item >>> list(map(my_not_function, lst)) [False, True, False, True]

Không sử dụng toán tử đảo ngược bitwise return not bool 3 trên booleans

Người ta có thể bị cám dỗ sử dụng toán tử đảo ngược bitwise return not bool 3 hoặc hàm toán tử tương đương return not bool 5 (hoặc một trong 3 bí danh khác ở đó). Nhưng vì return not bool 6 là một lớp con của return not bool 7, kết quả có thể là bất ngờ vì nó không trả về "Boolean nghịch đảo", nó trả về "số nguyên nghịch":

>>> ~True -2 >>> ~False -1

Đó là bởi vì return not bool 8 tương đương với return not bool 9 và >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 0 đến >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 1 và đảo ngược bitwise hoạt động trên biểu diễn bitwise của các số nguyên return not bool 9 và >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 1.integers return not bool 9 and >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 1.

Vì vậy, những thứ này không thể được sử dụng để "phủ định" một return not bool 6.

Phủ định với các mảng numpy (và các lớp con)

Nếu bạn đang xử lý các mảng numpy (hoặc các lớp con như >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 5 hoặc >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 6) có chứa booleans, bạn thực sự có thể sử dụng toán tử nghịch đảo bitwise (return not bool 3) để phủ nhận tất cả các booleans trong một mảng:all booleans in an array:

>>> import numpy as np >>> arr = np.array([True, False, True, False]) >>> ~arr array([False, True, False, True])

Hoặc chức năng numpy tương đương:

>>> np.bitwise_not(arr) array([False, True, False, True])

Bạn không thể sử dụng toán tử if bool == True: return False else: return True 5 hoặc hàm >>> import operator >>> operator.not_(False) True >>> operator.not_(True) False 9 trên các mảng numpy vì những điều này yêu cầu chúng trả về một return not bool 6 (không phải là một mảng booleans), tuy nhiên Numpy cũng chứa một hàm không hợp lý hoạt động yếu tố khôn ngoan:

>>> np.logical_not(arr) array([False, True, False, True])

Điều đó cũng có thể được áp dụng cho các mảng phi Boolean:

if bool == True: return False else: return True 0

Tùy chỉnh các lớp học của riêng bạn

if bool == True: return False else: return True 5 hoạt động bằng cách gọi return not bool 6 về giá trị và phủ nhận kết quả. Trong trường hợp đơn giản nhất, giá trị sự thật sẽ chỉ gọi >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 3 trên đối tượng.

Vì vậy, bằng cách thực hiện >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 3 (hoặc >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 5 trong Python 2), bạn có thể tùy chỉnh giá trị sự thật và do đó là kết quả của if bool == True: return False else: return True 5:

if bool == True: return False else: return True 1

Tôi đã thêm một câu lệnh >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 7 để bạn có thể xác minh rằng nó thực sự gọi phương thức:

if bool == True: return False else: return True 2

Tương tự như vậy, bạn có thể thực hiện phương thức >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 8 để thực hiện hành vi khi return not bool 3 được áp dụng:

if bool == True: return False else: return True 3

Một lần nữa với cuộc gọi >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 7 để xem nó thực sự được gọi là:

if bool == True: return False else: return True 4

Tuy nhiên, việc thực hiện >>> lst = [True, False, True, False] >>> list(map(operator.not_, lst)) [False, True, False, True] >>> lst = [True, False, True, False] >>> list(filter(operator.not_, lst)) [False, False] 8 như thế có thể gây nhầm lẫn vì hành vi của nó khác với hành vi python "bình thường". Nếu bạn từng làm điều đó tài liệu rõ ràng và đảm bảo rằng nó có trường hợp sử dụng khá tốt (và phổ biến).

Chủ đề