简体   繁体   English

python中的稀疏向量?

[英]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] . 稀疏向量是其条目几乎全为零的向量,例如[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 . 例如,较早显示的向量可以表示为{0:1, 7:2} ,因为它要表示的向量在索引0处具有值1 ,在索引7处具有值2 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? 有帮助吗?

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

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 (ie it is not 0, None, etc.). if val在压缩结束时表示,如果val的求值不为False(即它不是0,None等),则它将仅将键和值添加到字典中。

enumerate(v) goes through an iterable object (eg a list) and returns its index value together with the object/value at that location. enumerate(v)遍历一个可迭代的对象(例如列表),并返回其索引值以及该位置的对象/值。

n: val adds the value to the dictionary keyed on index value n (only if val is not zero/None). n: val将值添加到以索引值n键的字典中(仅当val不为零/无时)。

Your keys are all wrong. 您的钥匙全错了。 What you have right now is a dictionary of values in input -> last index that value appears . 您现在拥有的是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 . 您想要获得的是values in input: count of each valuevalues in input: count of each value字典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 ). 稀疏列表也不是(要得到第二个,将d[c] = k更改为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. enumerate()对此非常有用。 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. 但是,您需要做的是index in original list: value at that index有一个index in original list: value at that index字典index in original list: value at that index处的值(其中值不为0)。请仔细考虑每个变量的含义以及结果是什么。

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. 我会为您提供更多帮助(实际上,您可以在单个词典理解中完成此操作),但这确实看起来像是一个作业问题(即使不是,您自己解决该问题也有很多价值) ),因此我对发布完整答案表示怀疑。

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM