Solutions to Chapter 6 Exercises
Contents
10.4. Solutions to Chapter 6 Exercises#
10.4.1. Exercise 1#
Given the following dictionary, create a list of its keys and check whether the list contains a view of the keys or a copy of the keys. Hint: Try adding/removing entries and check what happens with the list.
dictionary_of_cars = {'Ferrari': 1939, 'Mercedes-Benz': 1926,
'Volkswagen': 1937, 'Audi':1909,
'BMW': 1916}
# write your code here
list_of_keys = list(dictionary_of_cars.keys())
print('List of keys before:', list_of_keys)
dictionary_of_cars.pop('Ferrari')
print('List of keys after:', list_of_keys)
print('Dictionary:', dictionary_of_cars)
List of keys before: ['Ferrari', 'Mercedes-Benz', 'Volkswagen', 'Audi', 'BMW']
List of keys after: ['Ferrari', 'Mercedes-Benz', 'Volkswagen', 'Audi', 'BMW']
Dictionary: {'Mercedes-Benz': 1926, 'Volkswagen': 1937, 'Audi': 1909, 'BMW': 1916}
As you can see, the list did not change although we removed the pair Ferrari:1939
from the dictionary. This implies that the list contains a copy of the keys and not a view of them.
10.4.2. Exercise 2#
Below you are given a list of names and a list of lists that contain the grades of some students. These lists are parallel. This means that the item at position 0 in the list_of_grades
corresponds to John
in the student
list and so on. Assuming that student names are unique, create a dictionary of student names and grades. Hint: Consider the zip() function.
students = ['John', 'Joe', 'Alisson', 'Ann']
list_of_grades = [[4.5,5,4.75], [4.75, 5.25, 5.5],
[4, 4.25, 5.75], [5.5, 5.5, 5.75]]
# write your code here
students_and_grades_dict = {name:list_of_grades for name, list_of_grades in zip(students, list_of_grades)}
students_and_grades_dict
{'John': [4.5, 5, 4.75],
'Joe': [4.75, 5.25, 5.5],
'Alisson': [4, 4.25, 5.75],
'Ann': [5.5, 5.5, 5.75]}
10.4.3. Exercise 3#
Below you are given a list of names and a list of lists that contain the grades of some students. These lists are parallel just as in the previous exercise. Assuming that student names are unique, create a dictionary of student names and their corresponding Grade Point Average (GPA).
students = ['John', 'Joe', 'Alisson', 'Ann']
list_of_grades = [[4.5,5,4.75], [4.75, 5.25, 5],
[4, 4.25, 6], [5.25, 5.5, 5.75]]
# write your code here
students_and_grades_dict = {name:sum(list_of_grades)/len(list_of_grades)
for name, list_of_grades in zip(students, list_of_grades)}
students_and_grades_dict
{'John': 4.75, 'Joe': 5.0, 'Alisson': 4.75, 'Ann': 5.5}
10.4.4. Exercise 4#
Find out if A is a superset of B using the issuperset()
method or the >
and >=
operators.
A = {1,2,3,4,5,6}
B = {2,4,6}
# write your code here
print(A.issuperset(B))
print(A > B)
print(A >= B)
True
True
True
10.4.5. Exercise 5 (Optional)#
Can a frozenset contain sets as its elements? What about sets? Can they contain frozensets as elements?
# write your answer here as a comment
# write your code here if you want to test your reasoning
fr_set = frozenset([{1,2,3,4,5,1}, {2,3,4,5}])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[9], line 2
1 # write your code here if you want to test your reasoning
----> 2 fr_set = frozenset([{1,2,3,4,5,1}, {2,3,4,5}])
TypeError: unhashable type: 'set'
# normal set contains frozen sets as elements
normal_set = {frozenset({1,2,3,4}), frozenset([1,2,3,4,5])}
normal_set
{frozenset({1, 2, 3, 4}), frozenset({1, 2, 3, 4, 5})}
Using the definitions of sets and frozensets we conclude that sets can contain frozensets as elements because elements of sets must be immutable, which is the case for frozensets. But, on the other hand, sets are mutable which means that they cannot be elements of a frozenset since frozensets cannot are entirely immutable (we cannot add/remove elements once they are created and also its elements cannot change). You get a TypeError
if you try to create a frozenset from a list of sets.
10.4.6. Exercise 6 (Optional)#
Given the text below, build a dictionary to map a word to the number of times it appears in the text. Then print only the characters that appear more than once. Also remove any empty character. Hint: Check the split() method. Source of text: “To be or not to be” soliloquy from Hamlet by Shakespeare.
text = 'To be, or not to be, that is the question: \
Whether tis nobler in the mind to suffer \
The slings and arrows of outrageous fortune, \
Or to take Arms against a Sea of troubles, \
And by opposing end them: to die, to sleep \
No more; and by a sleep, to say we end.'
# write your code here
list_of_words = list(filter(lambda word: word != '', text.split(' ')))
set_of_words = set(list_of_words)
dict_of_words = {}
for word in set_of_words:
dict_of_words[word] = list_of_words.count(word)
print(*((word, counter) for word, counter in dict_of_words.items() if counter > 1))
('be,', 2) ('the', 2) ('by', 2) ('of', 2) ('a', 2) ('and', 2) ('to', 6)
An example of a possible solution is shown above. If you have used another way and reached the same conclusion well done!