简体   繁体   English

在python中访问字典中的列表列表

[英]Accessing list of lists within a dictionary in python

i'm trying to write a function called compute. 我正在尝试编写一个称为计算的函数。

import math
def compute(index):
    res=0
    x=0
    res = sum(val[-1] * val[-1] for key,val in index.items())
    x = math.sqrt(res)
    return x

My output: 我的输出:

>>compute({'a': [0, 3], 'b': [0, 4]})
5.0

In the sample index above, document 0 has two terms 'a' (with tf-idf weight 3) and 'b' (with tf-idf weight 4). 在上面的样本索引中,文档0具有两个术语“ a”(tf-idf权重为3)和“ b”(tf-idf权重为4)。 It's length is therefore 5 = sqrt(9 + 16). 因此它的长度是5 = sqrt(9 + 16)。

Now how do i access list of lists elements within a dictionary ?? 现在如何访问字典中的列表元素列表? example, compute({'a': [[0, 3]], 'b': [[0, 4]]}) 例如, compute({'a': [[0, 3]], 'b': [[0, 4]]})

So that my desired output looks like this, 这样我想要的输出看起来像这样,

Desired output: 所需的输出:

>>length = compute(({'a': [[0, 3]], 'b': [[0, 4]]})
>>length[0]
5.0

so that the computation should happen for each list separately. 因此应该分别为每个列表进行计算。 As in, for example 例如

>>length = compute(({'a': [[0, 3],[1,3]], 'b': [[0, 4],[1,2]]})
>>length[1]
3.6

Can anyone suggest help to modify my function ??? 谁能建议帮助修改我的功能???

This is what you are looking for, as condensed as I think you can get it: 这就是您想要的,我认为很简洁,您可以得到:

import math

def compute(index):
    count = 0
    res=0
    listy = [] 
    for i in range(len( index[ index.keys()[0] ] )):
        res = sum(val[i][-1]*val[i][-1] for key,val in index.items())
        listy.append(math.sqrt(res))
    return listy

Output: 输出:

compute(({'a': [[0, 3],[1,3]], 'b': [[0, 4],[1,2]]})) 计算(({'a':[[0,3],[1,3]],'b':[[0,4],[1,2]]}))

=> [5.0, 3.605551275463989] => [5.0,3.605551275463989]

Essentially all you are doing is iterating over every element in the length of the key list in the first for loop, and with each iteration summing all of the squares of the value at position 1 for each respective dict . 本质上,您要做的就是遍历第一个for循环中key列表长度中的每个元素,并通过每次迭代求和每个dict在位置1处的所有值的平方。

If I understand the results you want correct: 如果我了解结果,则您要纠正:

import math

def compute_lengths(d):
    l = []

    for i in xrange(len( d[ d.keys()[0] ] )):
        ## len is number of inner lists within an outer list

        sum = 0
        for v in d.values():
            sum += (v[i][-1]) ** 2

        l.append(math.sqrt(sum))

    return l


d = {
    'a': [ [0, 3], [1, 3] ],
    'b': [ [0, 4], [1, 2] ]
}

l = compute_lengths(d)
print l

[5.0, 3.605551275463989]

You can certainly condense this code, but this is a straightforward approach that hopefully matches your algorithm. 您当然可以压缩此代码,但这是一种希望与您的算法匹配的简单方法。 Test this out for a larger dictionary, and see if it gives you what you want. 测试一下以获得更大的字典,看看它是否能满足您的需求。

I think what you're trying to say is (using x and y instead to avoid confusion) 我认为您要说的是(使用x和y以避免混淆)

length = sqrt((x[1] - x[0])^2 + (y[1] - y[0])^2)

and for 和为

x = [4,7]
y = [5,9]
length = 5

but you have an extra order of lists. 但是您还有一个额外的列表顺序。 So you just index it again like you normally would. 因此,您只需像往常一样再次对其进行索引。

def compute(index):
    output = []
    for i in range(0,len(index['a'])): #no error checking to ensure len(index['a']) == len(index['b'])
        output.append(math.sqrt(math.pow(index['a'][i][1]-index['a'][i][0],2)+math.pow(index['b'][i][1]-index['b'][i][0],2)))
    return output

Also, 也,

length = compute(({'a': [[0, 3],[1,3]], 'b': [[0, 4],[1,2]]}) 长度=计算(({'a':[[0,3],[1,3]],'b':[[0,4],[1,2]]})

Needs a closing paren, and 需要结束语,并且

length = compute(({'a': [[0, 3],[1,3]], 'b': [[0, 4],[1,2]]}) 长度=计算(({'a':[[0,3],[1,3]],'b':[[0,4],[1,2]]})

length[1] 长度[1]

length[1] = 2.23 长度[1] = 2.23

if my math assumption above was correct. 如果我上面的数学假设是正确的。 If not, you need to explain what you're doing more clearly 如果没有,您需要更清楚地说明自己在做什么

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

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