सेट थ्योरी

Efficient Python Code लिखना

Logan Thomas

Scientific Software Technical Trainer, Enthought

सेट थ्योरी

  • वस्तुओं के समूहों पर लागू गणित की शाखा
    • यानी sets
  • Python में बिल्ट-इन set datatype और इनके methods हैं:
    • intersection(): दोनों sets में मौजूद सभी elements
    • difference(): एक set में, दूसरे में नहीं
    • symmetric_difference(): ठीक एक set में मौजूद elements
    • union(): किसी एक set में मौजूद सभी elements
  • तेज़ membership testing
    • देखें कि कोई value sequence में है या नहीं
    • in ऑपरेटर का उपयोग करें
Efficient Python Code लिखना

लूप्स से ऑब्जेक्ट्स की तुलना

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

alt="एक बॉक्स "List A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "List B" में Caterpie, Pidgey, Squirtle दिखाए गए Pokémon"

Efficient Python Code लिखना

लूप्स से ऑब्जेक्ट्स की तुलना

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

alt="एक बॉक्स "List A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "List B" में Caterpie, Pidgey, Squirtle; दोनों बॉक्स में Squirtle घेरा गया है"

Efficient Python Code लिखना
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']
Efficient Python Code लिखना
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'}
Efficient Python Code लिखना

सेट थ्योरी से मिली दक्षता

%%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)
Efficient Python Code लिखना

Set method: difference

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

alt="एक बॉक्स "Set A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "Set B" में Caterpie, Pidgey, Squirtle; "Set A" में Bulbasaur और Charmander घेरा गया है"

Efficient Python Code लिखना

Set method: difference

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

alt="एक बॉक्स "Set A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "Set B" में Caterpie, Pidgey, Squirtle; "Set B" में Caterpie और Pidgey घेरा गया है"

Efficient Python Code लिखना

Set method: symmetric difference

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

alt="एक बॉक्स "Set A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "Set B" में Caterpie, Pidgey, Squirtle; Bulbasaur, Charmander, Caterpie और Pidgey घेरा गए हैं"

Efficient Python Code लिखना

Set method: union

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

alt="एक बॉक्स "Set A" में Bulbasaur, Charmander, Squirtle और अलग बॉक्स "Set B" में Caterpie, Pidgey, Squirtle; सभी Pokémon घेरा गए हैं और Squirtle केवल एक बार घेरा गया है"

Efficient Python Code लिखना

Sets के साथ membership testing

# हर डेटा स्ट्रक्चर में वही 720 कुल Pokémon
names_list  = ['Abomasnow', 'Abra', 'Absol', ...]
names_tuple = ('Abomasnow', 'Abra', 'Absol', ...)
names_set   = {'Abomasnow', 'Abra', 'Absol', ...}

alt="Abomasnow, Abra, Absol नामक Pokémon तीन अलग बॉक्स में दिखे, जिनके शीर्षक क्रमशः List, Tuple, और Set हैं"

Efficient Python Code लिखना

Sets के साथ membership testing

# हर डेटा स्ट्रक्चर में वही 720 कुल Pokémon
names_list  = ['Abomasnow', 'Abra', 'Absol', ...]
names_tuple = ('Abomasnow', 'Abra', 'Absol', ...)
names_set   = {'Abomasnow', 'Abra', 'Absol', ...}

alt="Abomasnow, Abra, Absol तीन अलग बॉक्स (List, Tuple, Set) में; Zubat नामक Pokémon से हर बॉक्स तक एक रेखा, जो प्रत्येक के लिए membership test दिखाती है"

Efficient Python Code लिखना
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)
Efficient Python Code लिखना

Sets से uniques

# 720 Pokémon के primary types, प्रत्येक 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']
Efficient Python Code लिखना

Sets से uniques

# 720 Pokémon के primary types, प्रत्येक 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'}
Efficient Python Code लिखना

आइए set थ्योरी का अभ्यास करें!

Efficient Python Code लिखना

Preparing Video For Download...