ravenwingz
ravenwingz

Reputation: 117

sparse vector in python?

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?

Upvotes: 3

Views: 10875

Answers (6)

Vijay Manickam
Vijay Manickam

Reputation: 1

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}
 {}

Upvotes: 0

eyoeldefare
eyoeldefare

Reputation: 2293

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)

Upvotes: 0

ted
ted

Reputation: 14684

def convertVector(numbers):
    dic = {}    
    for i in range(len(numbers)):
        if numbers[i] != 0:
            dic[i] = numbers[i]
    return dic

Upvotes: 2

Dima  Kudosh
Dima Kudosh

Reputation: 7366

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])

Upvotes: 4

Alex Van Liew
Alex Van Liew

Reputation: 1369

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.

Upvotes: 1

Alexander
Alexander

Reputation: 109510

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).

Upvotes: 3

Related Questions