Trei verificări not in cu or
În această lecție vei învăța cum să păstrezi un element dacă cel puțin una dintre trei valori nu se află în lista sa de excluderi.
De ce folosim trei verificări not in cu or?
Operatorul or face condiția adevărată dacă cel puțin una dintre verificări este adevărată.
if (
candidate["technology"] not in blocked_technologies
or candidate["city"] not in blocked_cities
or candidate["level"] not in blocked_levels
):
print(candidate["name"]) Candidatul este păstrat dacă tehnologia, orașul sau nivelul său nu se află în lista de excluderi corespunzătoare.
Cum funcționează?
technology = "PHP" city = "Bucuresti" level = "Senior" blocked_technologies = ["PHP"] blocked_cities = ["Iasi"] blocked_levels = ["Senior"] print(technology not in blocked_technologies) print(city not in blocked_cities) print(level not in blocked_levels)
False True False
Doar verificarea orașului este adevărată. Deoarece folosim or, condiția completă este adevărată.
Exemplu complet
candidates = [
{
"name": "Ana",
"technology": "Java",
"city": "Iasi",
"level": "Senior"
},
{
"name": "Mihai",
"technology": "PHP",
"city": "Cluj",
"level": "Senior"
},
{
"name": "Ioana",
"technology": "PHP",
"city": "Iasi",
"level": "Junior"
},
{
"name": "Radu",
"technology": "PHP",
"city": "Iasi",
"level": "Senior"
}
]
blocked_technologies = ["PHP"]
blocked_cities = ["Iasi"]
blocked_levels = ["Senior"]
selected_candidates = []
for candidate in candidates:
if (
candidate["technology"] not in blocked_technologies
or candidate["city"] not in blocked_cities
or candidate["level"] not in blocked_levels
):
selected_candidates.append(candidate)
for candidate in selected_candidates:
print(candidate["name"]) Ana Mihai Ioana
Când nu este selectat elementul?
Ana este selectată deoarece tehnologia Java nu este blocată.
Mihai este selectat deoarece orașul Cluj nu este blocat.
Ioana este selectată deoarece nivelul Junior nu este blocat.
Radu nu este selectat deoarece toate cele trei valori sunt blocate. Toate verificările sunt false, deci condiția cu or este falsă.
Model general
Sintaxăfiltered_items = []
for item in items:
if (
item["first_key"] not in first_excluded_list
or item["second_key"] not in second_excluded_list
or item["third_key"] not in third_excluded_list
):
filtered_items.append(item) Exemplu cu produse
products = [
{
"name": "Laptop",
"category": "Electronics",
"brand": "Unknown",
"condition": "Used"
},
{
"name": "Mouse",
"category": "Accessories",
"brand": "Unknown",
"condition": "Used"
},
{
"name": "Monitor",
"category": "Electronics",
"brand": "Samsung",
"condition": "Used"
}
]
blocked_categories = ["Electronics"]
blocked_brands = ["Unknown"]
blocked_conditions = ["Used"]
selected_products = []
for product in products:
if (
product["category"] not in blocked_categories
or product["brand"] not in blocked_brands
or product["condition"] not in blocked_conditions
):
selected_products.append(product)
for product in selected_products:
print(product["name"]) Mouse Monitor
Greșeli frecvente
Folosești and în loc de or
if technology not in blocked_technologies and city not in blocked_cities and level not in blocked_levels:
Cu and, toate cele trei valori trebuie să nu fie blocate.
Folosești in în loc de not in
if technology in blocked_technologies:
Această condiție verifică dacă valoarea este blocată, nu dacă lipsește din lista de excluderi.
Crezi că toate verificările trebuie să fie adevărate
Cu or, este suficientă o singură verificare adevărată.
Rezumat
Prima verificarePrima valoare nu se află în prima listă.
A doua verificareA doua valoare nu se află în a doua listă.
A treia verificareA treia valoare nu se află în a treia listă.
Creează lista blocked_cities cu valorile "Iasi" și "Brasov". Creează lista blocked_formats cu valorile "On-site" și "Private". Creează lista blocked_audiences cu valorile "Developers" și "Students".
Exercițiul, editorul și verificarea automată se deblochează cu Python Avansat.

Alex Cristian
Iulian Pahom
Catalina Ioana Dan