Question
Im working in Python and Im pretty confused on how to work with a dictionary inside another dictionary. i dont really get how to implement
Im working in Python and Im pretty confused on how to work with a dictionary inside another dictionary. i dont really get how to implement the second dicitonary when i try to store the values from the file
MY CODE:
reviewer = 0 review = {} review[reviewer] = {}
with open ("review.txt") as file: for line in file: (keys, value) = line.rstrip().split(':') review[keys] = value
THE QUESTION
Assume that the following dictionary is given to you
review.txt
{'Nancy Pollock': {'Lawrence of Arabia': 2.5, 'Gravity': 3.5, 'The Godfather': 3.0, 'Prometheus': 3.5, 'For a Few Dollars More': 2.5, 'The Guns of Navarone': 3.0},'Jack Holmes': {'Lawrence of Arabia': 3.0, 'Gravity': 3.5, 'The Godfather': 1.5, 'Prometheus': 5.0, 'The Guns of Navarone': 3.0, 'For a Few Dollars More': 3.5}, 'Mary Doyle': {'Lawrence of Arabia': 2.5, 'Gravity': 3.0, 'Prometheus': 3.5, 'The Guns of Navarone': 4.0},'Doug Redpath': {'Gravity': 3.5, 'The Godfather': 3.0, 'The Guns of Navarone': 4.5, 'Prometheus': 4.0, 'For a Few Dollars More': 2.5},'Jill Brown': {'Lawrence of Arabia': 3.0, 'Gravity': 4.0, 'The Godfather': 2.0, 'Prometheus': 3.0, 'The Guns of Navarone': 3.0, 'For a Few Dollars More': 2.0}, 'Trevor Chappell': {'Lawrence of Arabia': 3.0, 'Gravity': 4.0, 'The Guns of Navarone': 3.0, 'Prometheus': 5.0, 'For a Few Dollars More': 3.5},'Peter': {'Gravity':4.5,'For a Few Dollars More':1.0,'Prometheus':4.0}}
The dictionary contains names of reviewers and their reviews of different movies. You are required to write a Python program that computes a similarity score between any two reviewers. A simple way to calculate a similarity score is to use Euclidean distances.
An example is given below: Trevor Chappells reviews: {'Lawrence of Arabia': 3.0, 'Gravity': 4.0, 'The Guns of Navarone': 3.0, 'Prometheus': 5.0, 'For a Few Dollars More': 3.5} Peters reviews: {'Gravity':4.5,'For a Few Dollars More':1.0,'Prometheus':4.0}
The Euclidean distance between Peter and Trevor Chappell is computed as follows:
1) Only consider the movies that both have reviewed.
2) Take the difference between corresponding reviews.
3) Sum the square of the differences
4) The square root of the sum of differences is the Euclidean score. The shorter the distance the closer the two reviewers.
For our example, this would be: (4.0 4.5)2 + (3.5 1.0)2 + (5.0 4.0)2 = 0.25 + 6.25 + 1 = 7.5 Euclidean distance is Square Root of 7.5 = 2.7386 Your program should provide two methods. The first will compute the similarity between two reviewers. The second will get the similarities between a given reviewer and all other reviewer.
For example, the results for the following calls are shown below: print(getSimilarity("Peter", "Trevor Chappell")) 2.7386127875258306 getSimilarities("Peter")
Peter Nancy Pollock 1.87
Peter Jill Brown 1.50
Peter Jack Holmes 2.87
Peter Trevor Chappell 2.74
Peter Mary Doyle 1.58
Peter Doug Redpath 1.80
Note that these methods will use the dictionary of reviews to compute similarities.
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