Trei verificări not in cu and
În această lecție vei învăța cum să păstrezi un element doar dacă trei valori diferite nu se află în trei liste de excluderi.
De ce folosim trei verificări not in cu and?
Uneori vrem să păstrăm un element doar dacă niciuna dintre cele trei valori verificate nu este exclusă.
if (
candidate["technology"] not in blocked_technologies
and candidate["city"] not in blocked_cities
and candidate["level"] not in blocked_levels
):
print(candidate["name"]) Candidatul este selectat doar dacă tehnologia, orașul și nivelul său nu se află în listele de excluderi.
Cum funcționează?
technology = "Java" city = "Bucuresti" level = "Junior" 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)
True True True
Toate cele trei verificări sunt adevărate. Deoarece folosim and, condiția completă este adevărată.
Exemplu complet
candidates = [
{
"name": "Ana",
"technology": "Java",
"city": "Bucuresti",
"level": "Junior"
},
{
"name": "Mihai",
"technology": "PHP",
"city": "Cluj",
"level": "Mid"
},
{
"name": "Ioana",
"technology": "Python",
"city": "Iasi",
"level": "Junior"
},
{
"name": "Radu",
"technology": "Java",
"city": "Cluj",
"level": "Senior"
}
]
blocked_technologies = ["PHP"]
blocked_cities = ["Iasi"]
blocked_levels = ["Senior"]
selected_candidates = []
for candidate in candidates:
if (
candidate["technology"] not in blocked_technologies
and candidate["city"] not in blocked_cities
and candidate["level"] not in blocked_levels
):
selected_candidates.append(candidate)
for candidate in selected_candidates:
print(candidate["name"], candidate["technology"]) Ana Java
Toate cele trei verificări sunt obligatorii
Mihai nu este selectat deoarece tehnologia PHP se află în lista tehnologiilor blocate.
Ioana nu este selectată deoarece orașul Iasi se află în lista orașelor blocate.
Radu nu este selectat deoarece nivelul Senior se află în lista nivelurilor blocate.
Când folosim and, o singură verificare falsă face întreaga condiție falsă.
Model general
Sintaxăfiltered_items = []
for item in items:
if (
item["first_key"] not in first_excluded_list
and item["second_key"] not in second_excluded_list
and item["third_key"] not in third_excluded_list
):
filtered_items.append(item) Exemplu cu produse
products = [
{
"name": "Laptop",
"category": "Electronics",
"brand": "Lenovo",
"condition": "New"
},
{
"name": "Mouse",
"category": "Accessories",
"brand": "Unknown",
"condition": "New"
},
{
"name": "Monitor",
"category": "Electronics",
"brand": "Samsung",
"condition": "Used"
}
]
blocked_categories = ["Furniture"]
blocked_brands = ["Unknown"]
blocked_conditions = ["Used"]
selected_products = []
for product in products:
if (
product["category"] not in blocked_categories
and product["brand"] not in blocked_brands
and product["condition"] not in blocked_conditions
):
selected_products.append(product)
for product in selected_products:
print(product["name"]) Laptop
Greșeli frecvente
Folosești or în loc de and
if technology not in blocked_technologies or city not in blocked_cities or level not in blocked_levels:
Astfel este suficient ca doar una dintre verificări să fie adevărată.
Folosești in în loc de not in
if technology in blocked_technologies:
Această condiție verifică dacă valoarea este blocată.
Uiți una dintre verificări
if technology not in blocked_technologies and city not in blocked_cities:
Această condiție nu mai verifică și nivelul.
Rezumat
Primul not inVerifică dacă prima valoare nu este exclusă.
Al doilea not inVerifică dacă a doua valoare nu este exclusă.
Al treilea not inVerifică dacă a treia valoare nu este exclusă.
Creează lista blocked_cities cu valorile "Iasi" și "Brasov". Creează lista blocked_types cu valorile "On-site" și "Temporary". Creează lista blocked_levels cu valorile "Senior" și "Lead".
Exercițiul, editorul și verificarea automată se deblochează cu Python Avansat.

Alex Cristian
Iulian Pahom
Catalina Ioana Dan