I have a dictionary in the format, merged= user: [movie, rating, gender] , an ex
ID: 3837587 • Letter: I
Question
I have a dictionary in the format, merged= user: [movie, rating, gender] , an example=> merged=6039: [926, 5, 'F']. Now there are 6040 users, 3952 movies, ratings 1-5 and gender 'F' or 'M'. I want to obtain the results which have the movie ID, and the avg ratings of it by 'F' or 'M'. Any solutions?
how the dictionary 'merged' is obtained:
for user in movieRating.keys():
gender=movieGender[movieRating[user][0]]
rating=movieRating[user][2]
movie=movieRating[user][1]
merged[user]=[movie,rating,gender]
#print '%s %s %s %s'%(user,movie,rating,gender)
print (merged)
Explanation / Answer
If i understand the question correctly, you want to get results based on their gender, please correct me if i am wrong.
In the piece of code you gave, we can understand the following things, movieRating is a 2D aray with user and rating given by him/her stored.
for user in movieRating.keys():
gender=movieGender[movieRating[user][0]] // Here you get back either M or F depending on the gender.
rating=movieRating[user][2]
movie=movieRating[user][1]
merged[user]=[movie,rating,gender]
#print '%s %s %s %s'%(user,movie,rating,gender)
print (merged)
Now, if you want to filter these results based on any of the above parameters,
just add a conditional check like
Float totalratingM,totalratingF=0;
int countM,countF=0;
for user in movieRating.keys(): {
if(movieGender[movieRating[user][0]].equals('M' ) {// for case m
totalratingM=totalratingM+movieRating[user][2] ;
countM++;
}
else{
totalratingF=totalratingF+movieRating[user][2] // for case F
countF++;
}
}
Float averageM= totalratingM/countM; //average rating for M
Float averageF= totalraingF/countF; // average rating for F
In the loop, stiore the total rating by adding all the ratings based on M or F, and then outside the loop just divide them with count to get average.
Let me know if i have misunderstood the requirement or if you have any questions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.