3

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?

3
  • 2
    Looks like you need to do some rubber duck debugging here (en.wikipedia.org/wiki/Rubber_duck_debugging). Explain what each line of your function does, and why it returns those values. You should be able to answer your own question. Commented Jul 30, 2015 at 19:46
  • what't the point to create a d threre? Commented Jul 30, 2015 at 19:51
  • i know why it's returning last index for the value, because of for loop , where as it should return index of the value, when it occur in dictionary, nd sorry for bad english :D Commented Jul 30, 2015 at 19:52

6 Answers 6

4
def convertVector(numbers):
    d = {}
    for k, c in enumerate(numbers):
        if c:
            d[k] = c  
    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])
Sign up to request clarification or add additional context in comments.

1 Comment

if c: is more pythonic.
3

A one liner using conditional dictionary comprehension:

def sparseVector(v): 
    return {n: val for n, val in enumerate(v) if val}

 v1 = [1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 4]
 v2 = [1, 0, 1 , 0, 2, 0, 1, 0, 0, 1, 0]
 v3 = [0, 0, 0, 0, 0]

 >>> [sparseVector(v) for v in [v1, v2, v3]]
 [{0: 1, 3: 2, 7: 3, 12: 4}, 
  {0: 1, 2: 1, 4: 2, 6: 1, 9: 1}, 
  {}]

if val at the end of the compression means that it will only add the key and value to the dictionary if val does not evaluate to False (i.e. it is not 0, None, etc.).

enumerate(v) goes through an iterable object (e.g. a list) and returns its index value together with the object/value at that location.

n: val adds the value to the dictionary keyed on index value n (only if val is not zero/None).

2 Comments

Yeah, but why? You ought to add an explanation of why this does the correct thing, since this problem is clearly a learning exercise.
can you please explain this ,?
2
def convertVector(numbers):
    dic = {}    
    for i in range(len(numbers)):
        if numbers[i] != 0:
            dic[i] = numbers[i]
    return dic

Comments

1

Your keys are all wrong. What you have right now is a dictionary of values in input -> last index that value appears. What you're trying to get is a dictionary of values in input: count of each value. Neither is a sparse list (and to get the second, change d[c] = k to d[c] += 1).

However, you're on the right track. What you need to do is go through the input list, keeping track of the index you're on and the number at that index. enumerate() is great for this. But, what you need to do is have a dictionary of index in original list: value at that index where the value is not 0. Try thinking about exactly what each of your variable means, as well as what the result turns out to be.

I would help you more (in fact, you could do this in a single dictionary comprehension), but this really looks like a homework problem (and even if it's not, there's a good deal of value to be had in working the problem through yourself), so I'm dubious about posting a full answer.

Comments

0

Look at some of the awesome advises people gave you above. One way to do the problem is:

    def convertVector(numbers):
        Value = []
        for number, w in enumerate(numbers):
            if w != 0:
                Value.append((number, w))
        return dict(Value)

Comments

0
dic1= [1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 4]
dic2= [1, 0, 1 , 0, 2, 0, 1, 0, 0, 1, 0]
dic3= [0, 0, 0, 0, 0]

def convertVector(dd):
mydict = {i:dd[i] for i in range(len(dd))  if dd[i] != 0}
print(mydict) 
             
 convertVector(dic1)
 convertVector(dic2)
 convertVector(dic3)

Output:

 {0: 1, 3: 2, 7: 3, 12: 4}
 {0: 1, 2: 1, 4: 2, 6: 1, 9: 1}
 {}

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.