1

So I've been trying this for a while now but just can't get it work. If you look in the picture you see 2 people in the x and y axes and both are the ratings they gave to movies. And the question is how can I calculate the manhattan distance between these to people.

enter image description here

so this is what I already have... edit i forgot to say that prefs is a dict with personname as key and and a second dict as the value. second dict contains movies as key and rating as value.. and the person1 and 2 are just strings with name which can be found in prefs

def sum_manhattan(prefs,person1,person2):
    """Calculates the Manhattan distance between two critics"""
    total = 0
    ##assume person1 is the x axes and person 2 is the y axes
    x = prefs[person1]
    y = prefs[person2]

    for movie in x:
        if movie in y:
            total = abs(x[movie]-y[movie])
    return total

any help is welcome :)

3
  • What exactly is not working? I see that in the loop you do total = abs(...). Is that what you do in your program or is it something you mistyped in the question? Commented Apr 17, 2013 at 14:36
  • Are we to assume that prefs, person1, and person2 are structured the same as they are in Programming Collective Intelligence (where the image was lifted from)? answers.oreilly.com/topic/… Commented Apr 17, 2013 at 14:39
  • i'm sorry.. yes that is exactly what it is but it is what my professor gave me and i forgot to say that prefs is a dict with personname as key and and a second dict as the value. second dict contains movies as key and rating as value.. and the person1 and 2 are just strings with name which can be found in prefs Commented Apr 17, 2013 at 14:52

1 Answer 1

1

Based on the link posted by Alkini, I'd say that you should replace

total = abs(x[movie]-y[movie])

with

total += abs(x[movie]-y[movie])

to make things work.

What the code you posted does is return the absolute value of the difference between the ratings of the last movie, while I think you need to add the ratings differences for all movies.

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

1 Comment

arg yea that is it! it's very stupid of me not to have seen that :P but anyways thank you!

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.