Типи даних у Python
Jason Myers
Instructor
cookies_eaten_today = ['chocolate chip', 'peanut butter', ...: 'chocolate chip', 'oatmeal cream', 'chocolate chip']types_of_cookies_eaten = set(cookies_eaten_today)print(types_of_cookies_eaten)
set(['chocolate chip', 'oatmeal cream', 'peanut butter'])
.add() додає по одному елементуtypes_of_cookies_eaten.add('biscotti')
types_of_cookies_eaten.add('chocolate chip')
print(types_of_cookies_eaten)
set(['chocolate chip', 'oatmeal cream', 'peanut butter', 'biscotti'])
.update() об'єднує з іншою множиною або спискомcookies_hugo_ate = ['chocolate chip', 'anzac']
types_of_cookies_eaten.update(cookies_hugo_ate)
print(types_of_cookies_eaten)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter', 'biscotti'])
.discard() безпечно вилучає елемент за значенням.pop() вилучає й повертає довільний елемент (KeyError, якщо порожньо)types_of_cookies_eaten.discard('biscotti')print(types_of_cookies_eaten)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter'])
types_of_cookies_eaten.pop()
types_of_cookies_eaten.pop()
'chocolate chip'
'anzac'
.union() повертає множину всіх назв («or»).intersection() знаходить спільні дані («and»)cookies_jason_ate = set(['chocolate chip', 'oatmeal cream', 'peanut butter']) cookies_hugo_ate = set(['chocolate chip', 'anzac'])cookies_jason_ate.union(cookies_hugo_ate)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter'])
cookies_jason_ate.intersection(cookies_hugo_ate)
set(['chocolate chip'])
.difference() знаходить елементи, що є в поточній множині, але відсутні в аргументі («-»)cookies_jason_ate.difference(cookies_hugo_ate)
set(['oatmeal cream', 'peanut butter'])
cookies_hugo_ate.difference(cookies_jason_ate)
set(['anzac'])
Типи даних у Python