I am trying to work out a problem here and have gotten stuck. Basically what I want to do is this: I am given a dictionary in the format:
dr= {'student1067': ['0', '0', '0'], 'student1068' : ['1', '2', '5'], 'student1069': ['7','6','2'],.....
dr is a dictionary where the keys are the student identities(eg: 'student1068'), and the values are lists of strings, where each element is a rating for a particular book.
Then I am given a similarities list in the format:
SimList= [('student1067', 40), ('student1068', 40), ('student1069', 35).......]
SimList is a list of tuples, where the first element of the tuple is a student identity, and the second element is a similarity rating.
What I basically want to do is to go through the first element of each tuple in SimList, and look for the same key in dr. If the first element and key match, I want to multiply all the elements of the dictionary value using the second element of the tuple from SimList. So, for example, for student 1067, the values in the dictionary would all be multiplied by 40. For student 1068, the values in the dictionary would all be multiplied by 40 as well. And for student 1069, all the values would be multiplied by 35. Finally, I want a list where all these values are added together.
student1067---40 * [0,0,0] = [0,0,0]
student1068---40 * [1,2,5] = [40,80,200]
student1069---35 * [7,6,2] = [245,210,70]
--------------------------------
Final List= [285, 290, 270]
SO basically I want that final list result from the addition of the individual indices values. The code I have up until now looks like this:
FinalList=[]
for item in SimList:
CurrentList=[]
if item[0] in dr:
CurrentList.append(item[0]*int(x) for x in dr[item[0]])
if FinalList==[]:
FinalList=CurrentList
else:
FinalList=[FinalList[i] + CurrentList[i] for i in range(len(FinalList))]
print FinalList
So FinalList is to be my final list, while CurrentList is the temporary list created for each student, which reverts back to the empty list everytime the function runs. However, I think I got the placement of the "if" and "else" statements for the FinalList wrong because my code is not working properly. Can you guys please help me?
Thanks a lot, and I am sorry for the long question.