Sets

decal·2023년 1월 5일
0

study notes

목록 보기
6/12

Ch.10 on Mimo - Sets

Using Sets

A set is a collection of values like lists, but does not allow for any duplicate values.

Store in a set to make sure a collection of value can't have any duplicates:

postcodes = {"SW1A", "SY3", "B44"}
print(postcodes)

output:

{'SW1A', 'SY3', 'B44'}

.add() adds value to a set

If a set already contains the value we try to add, nothing happens. (Code runs without an error.)

Unlike lists, set elements don't have indices since they are unordered. So, we can only check if a set contains an element with 'in' keyword. (Output type: boolean)

For loop

answer_options = {"yes", "no"}

for answer in answer_options:
 print(f"Option: {answer}")

Output:

Option: no
Option: yes

Sets and Lists

Lists can have duplicate elements unlike sets.
To delete duplicates, we can transform a list into a set by:

grocery_list = ["broccoli", "cereal", "milk", "broccoli"]
print(set(grocery_list))

Output:

{'broccoli', 'milk', 'cereal'}

We can save the set by storing a variable:

grocery_list = ["broccoli", "cereal", "milk", "broccoli"]

grocery_set = set(grocery_list)  
print(grocery_set)

Output:

{'broccoli', 'milk', 'cereal'}

Set Operations

len() can be used to get the size of a set like with lists.

friends = {"Emma", "Jen", "Rob", "Ed"}
print(len(friends))

Output: 4

Subset

friends = {"Emma", "Jen", "Rob", "Ed"}
chat = {"Jen", "Ed"}

'chat' is a subset of 'friends'

.issubset()

friends = {"Emma", "Jen", "Rob", "Ed"}
chat = {"Jen", "Ed"}

print(chat.issubset(friends))

Output: True

We can save the boolean given by .issubset() in a variable like the following:

friends = {"Emma", "Jen", "Rob", "Ed"}
study_group = {"Emma", "Lisa"}

are_friends = study_group.issubset(friends)
print(are_friends)

Output: False

.union()
We can join two sets like the following:

classmates = {"Sue", "Paul"}
friends = {"Don", "Sue"}

print(classmates.union(friends))

Output:

{'Don', 'Paul', 'Sue'}

There won't be any duplicates in the new set joined by the .union() because it is a set!

.intersection()
We can create a set of elements that are present in both sets like the following:

classmates = {"Sue", "Luke", "Paul"}
friends = {"Don", "Sue", "Luke"}

print(classmates.intersection(friends))

Output:

{'Sue', 'Luke'}

So, union() gives us ALL the elements in both sets, while intersection() gives us the COMMON ones.

And we can save them by storing them in variables like the following:

classmates = {"Sue", "Luke", "Paul"}
friends = {"Don", "Sue", "Luke"}


everybody = classmates.union(friends)
print(everybody)

common = classmates.intersection(friends)
print(common)

Output:

{'Sue', 'Luke', 'Paul', 'Don'}
{'Sue', 'Luke'}

.difference()
We use it to remove elements from set A that exists in set B like the following (= elements that left set has, but right set doesn't):

classmates = {"Sue", "Paul"}
friends = {"Don", "Sue"}

print(classmates.difference(friends))

Output:

{'Paul'}

You can subtract the other way too:

classmates = {"Sue", "Paul"}
friends = {"Don", "Sue"}

print(friends.difference(classmates))

Output:

{'Don'}

0개의 댓글