Regulile lui De Morgan
În această lecție vei învăța cum să rescrii o condiție negată într-o formă echivalentă, mai directă, folosind not in și and.
Ce sunt regulile lui De Morgan?
Regulile lui De Morgan ne ajută să transformăm o condiție negată într-o condiție echivalentă fără un not pus înaintea întregului grup.
not (value in first_list or value in second_list)
Este echivalent cu:
value not in first_list and value not in second_list
De ce se schimbă or în and?
Pentru ca un element să nu se afle în niciuna dintre cele două liste, trebuie să nu se afle în prima listă și să nu se afle în a doua listă.
city = "Cluj"
blocked_cities = ["Iasi", "Brasov"]
restricted_cities = ["Constanta"]
print(
city not in blocked_cities
and city not in restricted_cities
) True
Două forme echivalente
status = "Paid"
country = "Romania"
blocked_statuses = ["Cancelled", "Refunded"]
blocked_countries = ["Blocked", "Restricted"]
first_result = not (
status in blocked_statuses
or country in blocked_countries
)
second_result = (
status not in blocked_statuses
and country not in blocked_countries
)
print(first_result)
print(second_result) True True
Ambele expresii produc același rezultat. A doua variantă este adesea mai ușor de citit.
Exemplu complet
orders = [
{"id": 101, "status": "Paid", "country": "Romania"},
{"id": 102, "status": "Cancelled", "country": "Romania"},
{"id": 103, "status": "Paid", "country": "Blocked"},
{"id": 104, "status": "Pending", "country": "Germany"}
]
blocked_statuses = ["Cancelled", "Refunded"]
blocked_countries = ["Blocked", "Restricted"]
selected_orders = []
for order in orders:
if (
order["status"] not in blocked_statuses
and order["country"] not in blocked_countries
):
selected_orders.append(order)
for order in selected_orders:
print(order["id"], order["status"]) 101 Paid 104 Pending
Cum este evaluată fiecare comandă?
Comanda 101 este selectată deoarece statusul nu este blocat și țara nu este blocată.
Comanda 102 nu este selectată deoarece statusul Cancelled se află în lista statusurilor blocate.
Comanda 103 nu este selectată deoarece țara Blocked se află în lista țărilor blocate.
Comanda 104 este selectată deoarece ambele verificări not in sunt adevărate.
Model general
Transformarenot (
item["first_key"] in first_blocked_list
or item["second_key"] in second_blocked_list
) Poate fi rescris astfel:
item["first_key"] not in first_blocked_list and item["second_key"] not in second_blocked_list
Exemplu cu utilizatori
users = [
{"name": "Ana", "role": "Admin", "city": "Bucuresti"},
{"name": "Mihai", "role": "Blocked", "city": "Cluj"},
{"name": "Ioana", "role": "Editor", "city": "Restricted"}
]
blocked_roles = ["Blocked"]
blocked_cities = ["Restricted"]
selected_users = []
for user in users:
if user["role"] not in blocked_roles and user["city"] not in blocked_cities:
selected_users.append(user)
for user in selected_users:
print(user["name"]) Ana
Greșeli frecvente
Păstrezi or după transformare
status not in blocked_statuses or country not in blocked_countries
Această condiție poate accepta o comandă chiar dacă una dintre valori este blocată.
Negi doar prima verificare
status not in blocked_statuses and country in blocked_countries
A doua verificare trebuie transformată și ea în not in.
Folosești not înaintea întregului grup
not (status in blocked_statuses or country in blocked_countries)
Expresia este corectă, dar în această lecție exersăm forma echivalentă cu două verificări not in și and.
Rezumat
Negarea dispareEliminăm not din fața grupului.
in devine not inFiecare verificare este negată separat.
or devine andAmbele verificări negate trebuie să fie adevărate.
Creează lista blocked_statuses cu valorile "Rejected" și "Cancelled". Creează lista blocked_countries cu valorile "Restricted" și "Blocked". Creează lista goală selected_applications.
Exercițiul, editorul și verificarea automată se deblochează cu Python Avansat.

Alex Cristian
Iulian Pahom
Catalina Ioana Dan