set: Множества в Python

Contents
Введение
Превратить в словарь
Копировать словарь
Update: добавить один словарь к другому либо изменить значения
Iteration: перебор элементов словаря
Items: перебор кортежами из пар ключ-значение
in, not in, del
pprint: читаемый вывод
Похожие статьи

Введение

Множество - это неупорядоченная коллекция уникальных элементов.

Сами множества изменяемы (mutable).

Элементы множества должны быть неизменяемы (immutable).

Чтобы создать множество нужно воспользоваться фигурными скобками {}

{element1, element2}

p = {6, 28, 496, 8128, 33550336} print("p: ", p) print("type(p): ", type(p)) # Since {} creates empty dict use # set() to created empty set e = set() print("e: ", e) print("type(e): ", type(e)) # set can be created from a list s = set([2, 4,16, 64, 4096, 65536, 262144]) print("s: ", s) # it removes duplicates from list t = [1, 4, 2, 1, 7, 9, 9] print("t: ", t) print("set(t): ", set(t)) # sets are iterable for x in {1, 2, 4, 8, 16, 32}: print(x) # in and not in q = {2, 9, 6, 4} print("q: ", q) print("3 in q: ", 3 in q) print("3 not in q: ", 3 not in q) # adding elements k = {81, 108} print("k: ", k) k.add(54) print("k: ", k) k.add(12) print("k: ", k) k.add(108) print("k: ", k) k.update([37, 128, 97]) print("k: ", k) # removing elements # if element is not in set removing it # results in error, try: # k.remove(98) k.discard(98) print("k: ", k) # copy set j = k.copy() # shallow copy print("j: ", j) # algebra blue_eyes = {'Olivia', 'Harry', 'Lilly', 'Jack', 'Amelia'} blond_hair = {'Harry', 'Jack', 'Amelia', 'Mia', 'Joshua'} smell_hcn = {'Harry', 'Amelia'} taste_ptc = {'Harry', 'Lily', 'Amelia', 'Lola'} o_blood = {'Mia', 'Joshua', 'Lily', 'Olivia'} b_blood = {'Amelia', 'Jack'} a_blood = {'Harry'} ab_blood = {'Joshua', 'Lola'} # union print("blue_eyes: ", blue_eyes) print("blond_hair: ", blond_hair) print("blue_eyes.union(blond_hair): ", blue_eyes.union(blond_hair)) print(blue_eyes.union(blond_hair) == blond_hair.union(blue_eyes)) # intersection print("blue_eyes.intersection(blond_hair): ", blue_eyes.intersection(blond_hair)) print(blue_eyes.intersection(blond_hair) == blond_hair.intersection(blue_eyes)) # difference print("blond_hair.difference(blue_eyes): ", blond_hair.difference(blue_eyes)) print(blond_hair.difference(blue_eyes) == blue_eyes.difference(blond_hair)) # False # symmetric difference # it is commutative print("blond_hair.symmetric_difference(blue_eyes): ", blond_hair.symmetric_difference(blue_eyes)) print(blond_hair.symmetric_difference(blue_eyes) == blue_eyes.symmetric_difference(blond_hair)) # True # issubset, issuperset print("blond_hair: ", blond_hair) print("smell_hcn ", smell_hcn) print("smell_hcn.issubset(blond_hair): ", smell_hcn.issubset(blond_hair)) # True print("taste_ptc: ", taste_ptc) print("taste_ptc.issuperset(smell_hcn): ", taste_ptc.issuperset(smell_hcn)) # True # isdisjoint - no members in common print("a_blood: ", a_blood) print("o_blood: ", o_blood) print("a_blood.isdisjoint(o_blood): ", a_blood.isdisjoint(o_blood)) # True

p: {33550336, 8128, 496, 6, 28} type(p): <class 'set'> e: set() type(e): <class 'set'> s: {4096, 64, 2, 65536, 4, 262144, 16} t: [1, 4, 2, 1, 7, 9, 9] set(t): {1, 2, 4, 7, 9} 32 1 2 4 8 16 q: {9, 2, 4, 6} 3 in q: False 3 not in q: True k: {81, 108} k: {81, 108, 54} k: {81, 108, 12, 54} k: {81, 108, 12, 54} k: {128, 97, 37, 108, 12, 81, 54} k: {128, 97, 37, 108, 12, 81, 54} j: {128, 97, 81, 37, 54, 108, 12} blue_eyes: {'Lilly', 'Jack', 'Amelia', 'Olivia', 'Harry'} blond_hair: {'Jack', 'Amelia', 'Mia', 'Joshua', 'Harry'} blue_eyes.union(blond_hair): {'Amelia', 'Olivia', 'Lilly', 'Jack', 'Mia', 'Joshua', 'Harry'} True blue_eyes.intersection(blond_hair): {'Jack', 'Amelia', 'Harry'} True blond_hair.difference(blue_eyes): {'Joshua', 'Mia'} False blond_hair.symmetric_difference(blue_eyes): {'Lilly', 'Mia', 'Joshua', 'Olivia'} True blond_hair: {'Jack', 'Amelia', 'Mia', 'Joshua', 'Harry'} smell_hcn {'Amelia', 'Harry'} smell_hcn.issubset(blond_hair): True taste_ptc: {'Lola', 'Lily', 'Amelia', 'Harry'} taste_ptc.issuperset(smell_hcn): True a_blood: {'Harry'} o_blood: {'Mia', 'Olivia', 'Lily', 'Joshua'} a_blood.isdisjoint(o_blood): True

Related Articles
Built-in Collections
Строки
f-string
format string
Списки []
list comprehension: Абстракция списка
Словари {:}
dict comprehension: Абстракция словаря
Множества {}
set comprehension: Абстракция множества
Кортежи ()
Протоколы
Итерация
Python
if, elif, else
Циклы

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedbak and Questions in Telegram

@aofeedchat