Mengenlehre

Effizienten Python-Code schreiben

Logan Thomas

Scientific Software Technical Trainer, Enthought

Mengenlehre

  • Ein Bereich der Mathematik, der sich mit Mengen von Objekten beschäftigt.
    • insbesondere sets – Mengen
  • Python hat den Datentyp set , mit den dazugehörigen Methoden:
    • intersection(): alle Elemente, die in beiden Mengen vorkommen
    • difference(): alle Elemente in einer Menge, aber nicht in der anderen
    • symmetric_difference(): das Gegenteil der Schnittmenge
    • union(): einzigartige Elemente in beiden Mengen
  • Schneller Zugehörigkeitstest
    • Prüft, ob ein Wert in einer Sequenz vorkommt oder nicht.
    • Nutzung des in Operators
Effizienten Python-Code schreiben

Objekte mit Schleifen vergleichen

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”

Effizienten Python-Code schreiben

Objekte mit Schleifen vergleichen

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”

Effizienten Python-Code schreiben
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']
Effizienten Python-Code schreiben
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'}
Effizienten Python-Code schreiben

Effizienzgewinne mit der Mengenlehre

%%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)
Effizienten Python-Code schreiben

Set Methode: difference – Unterschied

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”

Effizienten Python-Code schreiben

Set Methode: difference – Unterschied

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”

Effizienten Python-Code schreiben

Set Methode: symmetric difference – symmetrische Differenz

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”

Effizienten Python-Code schreiben

Set Methode: union – Vereinigung

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”

Effizienten Python-Code schreiben

Zugehörigkeitstest mit Sets

# 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”

Effizienten Python-Code schreiben

Zugehörigkeitstest mit Sets

# 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”

Effizienten Python-Code schreiben
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)
Effizienten Python-Code schreiben

Einzigartige Elemente mit Sets finden

# 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']
Effizienten Python-Code schreiben

Einzigartige Elemente mit Sets finden

# 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'}
Effizienten Python-Code schreiben

Lass uns die Mengenlehre üben!

Effizienten Python-Code schreiben

Preparing Video For Download...