0

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 ???

3
  • One question : So for your output, you want one single value or a different value for every respective key? Commented Feb 13, 2015 at 19:51
  • if i call length[1] then it should multiply 3*3 = 9 and 2*2=4 and add them up 9+4=13 and then take a square root of 13 , which is 3.605 in my above example..the first element in each list is a document id in which the term 'a' or 'b' occurs for some particular no.of times that is represented as the second element in each individual list. Commented Feb 13, 2015 at 19:58
  • Oh, I see I see. Working on it Commented Feb 13, 2015 at 20:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm supposed to use only one parameter. can you edit your function to satisfy my condition plz and can you show me your output
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.

Comments

0

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

3 Comments

your function calculates only for term 'a' and 'b' in a prefixed way, what change has to be done if there are many terms within the dictionary ?? such as 'c' and 'd' likewise..can we use a for loop for that ?
moreover its not x[1]-x[0]^2 concept here, the first entry in the list is the document id followed by number of times the term 'a' or 'b' or whatever term appears in that particular document..so we should only take the second element of each list within consideration and perform calculation..
In this case, please update your question to include all the details so a complete and proper answer can be given.

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.