A sparse vector is a vector whose entries are almost all zero, like [1, 0, 0, 0, 0, 0, 0, 2, 0]. Storing all those zeros wastes memory and dictionaries are commonly used to keep track of just the nonzero entries. For example, the vector shown earlier can be represented as {0:1, 7:2}, since the vector it is meant to represent has the value 1 at index 0 and the value 2 at index 7. Write a function that converts a sparse vector into a dictionary as described above.
Examples
>>> convertVector([1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 4])
{0: 1, 3: 2, 7: 3, 12: 4}
>>> convertVector([1, 0, 1 , 0, 2, 0, 1, 0, 0, 1, 0])
{0: 1, 2: 1, 4: 2, 6: 1, 9: 1}
>>> convertVector([0, 0, 0, 0, 0])
{}
My Code
def convertVector(numbers):
d = {i: 0 for i in numbers}
for k, c in enumerate(numbers):
d[c] = k # increment its value
return d
print convertVector([1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 4])
print convertVector([1, 0, 1 , 0, 2, 0, 1, 0, 0, 1, 0])
print convertVector([0, 0, 0, 0, 0])
Code Returning it as
{0: 11, 1: 0, 2: 3, 3: 7, 4: 12}
{0: 10, 1: 9, 2: 4}
{0: 4}
The problem is it's returning last index, correspond to the value. where as it should return as
{0: 1, 3: 2, 7: 3, 12: 4}
{0: 1, 2: 1, 4: 2, 6: 1, 9: 1}
{}
Any Help?
dthrere?