Sorry to start another thread but I misunderstood the original problem given to us so I am trying not to get it mixed up.
I'm having a really hard time understanding how this assignment is supposed to be completed. We are supposed to take, as arguments, two sparse vectors as dictionaries and add them together. For example:
CODE
dicA = {len: 10, 2:5, 8:3} = 0, 0, 5, 0, 0, 0, 0, 0, 3, 0, 0
dicB = {len: 10, 1:4, 8:1} = 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
A+B ={len:10, 1:4, 2:5, 8:4} = 4, 0, 5, 0, 0, 0, 0, 0, 4, 0, 0
What I think I am supposed to do is take the first dictionary and populate all the keys < len with zeros, if they do not already have a value. My question is, how do I take {len: 10, 1:4, 8:1} and turn it into [4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]?
My other thought was to make an additional step and place all the keys into a list then sort the list to be sure I was retrieving the correct element from the dictionary. Something like this:
CODE
total_dic = {}
key1, key2 = dic1.keys(), dic2.keys()
key1.sort()
key2.sort()
i = 0
total = []
while i < len(key1):
#returns total as a dictionary
total_dic[i] = dic1[key1[i]] + dic2[key2[i]]
i = i+1