Regulile lui De Morgan cu trei condiții
În această lecție vei extinde regulile lui De Morgan la trei verificări și vei transforma o condiție negată într-o expresie echivalentă.
De la două la trei condiții
Regulile lui De Morgan funcționează și atunci când avem trei sau mai multe condiții.
not (
first_condition
or second_condition
or third_condition
) Este echivalent cu:
not first_condition and not second_condition and not third_condition
Exemplu cu apartenență
city = "Cluj"
role = "Developer"
status = "Active"
blocked_cities = ["Iasi"]
blocked_roles = ["Blocked"]
blocked_statuses = ["Suspended"]
result = (
city not in blocked_cities
and role not in blocked_roles
and status not in blocked_statuses
)
print(result) True
Toate cele trei verificări not in sunt adevărate, deci rezultatul final este True.
Forma negată și forma transformată
first_result = not (
city in blocked_cities
or role in blocked_roles
or status in blocked_statuses
)
second_result = (
city not in blocked_cities
and role not in blocked_roles
and status not in blocked_statuses
)
print(first_result)
print(second_result) True True
Cele două expresii sunt echivalente. Fiecare verificare este negată separat, iar or devine and.
Exemplu complet cu dicționare
accounts = [
{"name": "Ana", "role": "Admin", "country": "Romania", "status": "Active"},
{"name": "Mihai", "role": "Blocked", "country": "Romania", "status": "Active"},
{"name": "Ioana", "role": "Editor", "country": "Restricted", "status": "Active"},
{"name": "Radu", "role": "Editor", "country": "Germany", "status": "Suspended"},
{"name": "Elena", "role": "Editor", "country": "Germany", "status": "Active"}
]
blocked_roles = ["Blocked"]
blocked_countries = ["Restricted"]
blocked_statuses = ["Suspended"]
selected_accounts = []
for account in accounts:
if (
account["role"] not in blocked_roles
and account["country"] not in blocked_countries
and account["status"] not in blocked_statuses
):
selected_accounts.append(account)
for account in selected_accounts:
print(account["name"], account["status"]) Ana Active Elena Active
Cum este evaluat fiecare cont?
Ana este selectată deoarece rolul, țara și statusul nu sunt blocate.
Mihai nu este selectat deoarece rolul Blocked se află în lista rolurilor blocate.
Ioana nu este selectată deoarece țara Restricted se află în lista țărilor blocate.
Radu nu este selectat deoarece statusul Suspended se află în lista statusurilor blocate.
Elena este selectată deoarece toate cele trei verificări sunt adevărate.
Model general
Transformarenot (
item["first_key"] in first_blocked_list
or item["second_key"] in second_blocked_list
or item["third_key"] in third_blocked_list
) Poate fi rescris astfel:
item["first_key"] not in first_blocked_list and item["second_key"] not in second_blocked_list and item["third_key"] not in third_blocked_list
Greșeli frecvente
Folosești or între verificările negate
role not in blocked_roles or country not in blocked_countries or status not in blocked_statuses
Această expresie poate accepta un element chiar dacă una dintre valori este blocată.
Negi doar două dintre cele trei condiții
role not in blocked_roles and country not in blocked_countries and status in blocked_statuses
Toate cele trei verificări trebuie transformate în not in.
Folosești un singur and
first_condition and second_condition third_condition
Pentru trei verificări sunt necesari exact doi operatori and.
Rezumat
Trei verificăriFiecare condiție este analizată separat.
Trei not inFiecare verificare de apartenență este negată.
Două andToate cele trei verificări trebuie să fie adevărate.
Creează lista blocked_categories cu valoarea "Deprecated". Creează lista blocked_levels cu valoarea "Expert". Creează lista blocked_statuses cu valoarea "Archived".
Exercițiul, editorul și verificarea automată se deblochează cu Python Avansat.

Alex Cristian
Iulian Pahom
Catalina Ioana Dan