Teoria mulțimilor

Scriere eficientă a codului Python

Logan Thomas

Scientific Software Technical Trainer, Enthought

Teoria mulțimilor

  • Ramură a matematicii aplicată colecțiilor de obiecte
    • adică sets
  • Python are tipul de date set cu metode asociate:
    • intersection(): toate elementele comune ambelor mulțimi
    • difference(): elementele dintr-o mulțime, absente din cealaltă
    • symmetric_difference(): elementele din exact una dintre mulțimi
    • union(): toate elementele din oricare dintre mulțimi
  • Testare rapidă a apartenenței
    • Verifică dacă o valoare există într-o secvență
    • Folosind operatorul in
Scriere eficientă a codului Python

Compararea obiectelor cu bucle

list_a = ['Bulbasaur', 'Charmander', 'Squirtle']
list_b = ['Caterpie', 'Pidgey', 'Squirtle']

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled List A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled List B"

Scriere eficientă a codului Python

Compararea obiectelor cu bucle

list_a = ['Bulbasaur', 'Charmander', 'Squirtle']
list_b = ['Caterpie', 'Pidgey', 'Squirtle'] 

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled List A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled List B; Squirtle is circled in both boxes"

Scriere eficientă a codului Python
list_a = ['Bulbasaur', 'Charmander', 'Squirtle']
list_b = ['Caterpie', 'Pidgey', 'Squirtle'] 
in_common = []

for pokemon_a in list_a:
    for pokemon_b in list_b:
        if pokemon_a == pokemon_b:
            in_common.append(pokemon_a)

print(in_common)
['Squirtle']
Scriere eficientă a codului Python
list_a = ['Bulbasaur', 'Charmander', 'Squirtle']
list_b = ['Caterpie', 'Pidgey', 'Squirtle'] 
set_a = set(list_a)
print(set_a)
{'Bulbasaur', 'Charmander', 'Squirtle'}
set_b = set(list_b)
print(set_b)
{'Caterpie', 'Pidgey', 'Squirtle'}
set_a.intersection(set_b)
{'Squirtle'}
Scriere eficientă a codului Python

Eficiență prin teoria mulțimilor

%%timeit
in_common = []

for pokemon_a in list_a:
    for pokemon_b in list_b:
        if pokemon_a == pokemon_b:
            in_common.append(pokemon_a)
601 ns ± 17.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit in_common = set_a.intersection(set_b)
137 ns ± 3.01 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Scriere eficientă a codului Python

Metoda mulțimilor: difference

set_a = {'Bulbasaur', 'Charmander', 'Squirtle'}
set_b = {'Caterpie', 'Pidgey', 'Squirtle'}
set_a.difference(set_b)
{'Bulbasaur', 'Charmander'}

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled Set A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled Set B; Bulbasaur and Charmander are circled in the box titled Set A"

Scriere eficientă a codului Python

Metoda mulțimilor: difference

set_a = {'Bulbasaur', 'Charmander', 'Squirtle'}
set_b = {'Caterpie', 'Pidgey', 'Squirtle'}
set_b.difference(set_a)
{'Caterpie', 'Pidgey'}

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled Set A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled Set B; Caterpie and Pidgey are circled in the box titled Set B"

Scriere eficientă a codului Python

Metoda mulțimilor: symmetric difference

set_a = {'Bulbasaur', 'Charmander', 'Squirtle'}
set_b = {'Caterpie', 'Pidgey', 'Squirtle'}
set_a.symmetric_difference(set_b)
{'Bulbasaur', 'Caterpie', 'Charmander', 'Pidgey'}

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled Set A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled Set B; Bulbasaur, Charmander, Caterpie and Pidgey are circled"

Scriere eficientă a codului Python

Metoda mulțimilor: union

set_a = {'Bulbasaur', 'Charmander', 'Squirtle'}
set_b = {'Caterpie', 'Pidgey', 'Squirtle'}
set_a.union(set_b)
{'Bulbasaur', 'Caterpie', 'Charmander', 'Pidgey', 'Squirtle'}

alt="The Pokémon named Bulbasaur, Charmander, and Squirtle enclosed in a box titled Set A and the Pokémon Caterpie, Pidgey, and Squirtle enclosed in a separate box titled Set B; All  Pokémon are circled and Squirtle only circled once"

Scriere eficientă a codului Python

Testarea apartenenței cu mulțimi

# The same 720 total Pokémon in each data structure
names_list  = ['Abomasnow', 'Abra', 'Absol', ...]
names_tuple = ('Abomasnow', 'Abra', 'Absol', ...)
names_set   = {'Abomasnow', 'Abra', 'Absol', ...}

alt="The Pokémon named Abomasnow, Abra, and Absol enclosed in three separate boxes each titled List, Tuple, and Set respectively"

Scriere eficientă a codului Python

Testarea apartenenței cu mulțimi

# The same 720 total Pokémon in each data structure
names_list  = ['Abomasnow', 'Abra', 'Absol', ...]
names_tuple = ('Abomasnow', 'Abra', 'Absol', ...)
names_set   = {'Abomasnow', 'Abra', 'Absol', ...}

alt="The Pokémon named Abomasnow, Abra, and Absol enclosed in three separate boxes each titled List, Tuple, and Set respectively; the Pokémon named Zubat with a line drawn to each box representing a membership test for each box"

Scriere eficientă a codului Python
names_list  = ['Abomasnow', 'Abra', 'Absol', ...]
names_tuple = ('Abomasnow', 'Abra', 'Absol', ...)
names_set   = {'Abomasnow', 'Abra', 'Absol', ...}
%timeit 'Zubat' in names_list
7.63 µs ± 211 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit 'Zubat' in names_tuple
7.6 µs ± 394 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit 'Zubat' in names_set
37.5 ns ± 1.37 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Scriere eficientă a codului Python

Valori unice cu mulțimi

# 720 Pokémon primary types corresponding to each Pokémon
primary_types = ['Grass', 'Psychic', 'Dark', 'Bug', ...]
unique_types = []

for prim_type in primary_types:
    if prim_type not in unique_types:
        unique_types.append(prim_type)

print(unique_types)
['Grass', 'Psychic', 'Dark', 'Bug', 'Steel', 'Rock', 'Normal',
 'Water', 'Dragon', 'Electric', 'Poison', 'Fire', 'Fairy', 'Ice',
 'Ground', 'Ghost', 'Fighting', 'Flying']
Scriere eficientă a codului Python

Valori unice cu mulțimi

# 720 Pokémon primary types corresponding to each Pokémon
primary_types = ['Grass', 'Psychic', 'Dark', 'Bug', ...]
unique_types_set = set(primary_types)
print(unique_types_set)
{'Grass', 'Psychic', 'Dark', 'Bug', 'Steel', 'Rock', 'Normal',
 'Water', 'Dragon', 'Electric', 'Poison', 'Fire', 'Fairy', 'Ice',
 'Ground', 'Ghost', 'Fighting', 'Flying'}
Scriere eficientă a codului Python

Să exersăm teoria mulțimilor!

Scriere eficientă a codului Python

Preparing Video For Download...