1

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.

0

2 Answers 2

3
FinalList = [(k, [int(i)*v for i in dr[k]]) for k, v in SimList]
TotalList = [sum(x) for x in zip(*(j for i in FinalList for j in i[1:]))]
Sign up to request clarification or add additional context in comments.

2 Comments

okay this will update dr, so that the value for each student key will be the current list values. How do you recommend I do the final part and add all the elements in each value list depending on the indices?
Thanks a lot! Appreciate it.
0

This works for me:

dr= {'student1067': ['0', '0', '0'], 'student1068' : ['1', '2', '5'], 'student1069': ['7','6','2']}
SimList= [('student1067', 40), ('student1068', 40), ('student1069', 35)]
FinalList=[]

for item in SimList:
    CurrentList = []
    if(item[0] in dr):
        CurrentList.append([ item[1] * 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

Turns out, you used item[0] for multiplication instead of item[1] while appending list into CurrentList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.