简体   繁体   中英

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). It's length is therefore 5 = sqrt(9 + 16).

Now how do i access list of lists elements within a dictionary ?? example, 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]]}))

=> [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 .

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)

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

Needs a closing paren, and

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

length[1]

length[1] = 2.23

if my math assumption above was correct. If not, you need to explain what you're doing more clearly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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