Question
A set is a collection of distinct elements, that is, each element is unique. Searching over sets is very fast and they can be used
A set is a collection of distinct elements, that is, each element is unique. Searching over sets is very fast and they can be used to find the unique items in a collection.
Cell 1:
cat_names = {"Kitty", "Garfield", "Horse"}
dog_names = {"Fido", "Odie", "Satchel"}
pet_names = cat_names|dog_names # the union of the two sets
print("pet_names = ", pet_names)
canine_names = pet_names - cat_names # the difference of the two sets
print("canine_names = ", canine_names)
Cell 2:
list1 = list(range(1,11)) # list1 is a list
print("list 1 = ", list1)
set1 = set(list1) # cast list1 as a set
print("set 1 = ", set1)
set2 = set() # empty set
for i in range(6, 16):
set2.add(i)
print("set 2 = ", set2)
set3 = set1|set2
print("set 3 = ", set3)
6 in set3
# In the code cells above that were used to perform the word count, bookcontents is a variable that stores all of
# the words in the book as a list. Enter Python code below to find the length of the list. (How many words are in the book?)
# Cast bookcontents as a set and find the length of the set. (How many unique words are in the book?)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started