2

I'm trying to load my data so it stores in a dictionary of dictionaries. But I'm getting this error:

  self.user_rating(mid, {} )   #Dictionary of dictionaries = urating
  TypeError: 'dict' object is not callable

This is the code for this particular method.

def load_r(self,file):
            f = open(file)
            for line in f:
                    line = line.rstrip()
                    components = line.split("::")
                    uid = components[0]
                    mid = components[1]
                    urating = components[2]
            self.user_rating(mid, {} )  ###Line of interest   
            f.close()

The dictionary is supposed to be something like

GOAL: dictionary[mid, dictionary2[uid, urating]] as in dictionary[key, dictionary2[key, value]]

Am I supposed to set the self.user_rating line equal to something? Thank you!


EDIT (update): When I try to call

self.mdb.set_user_m_rating(41, 787, 2)

I get an error in the line

self.user_rating[mid][uid] = rating

This is after changing the line to self.user_rating[mid] = {} but later calling self.user_rating[mid][uid]=rating in another method.

4
  • 3
    Do you mean self.user_rating[mid] = {}? Feb 17, 2014 at 3:38
  • With that, can I do this?--------------------------------------------- def set_user_movie_rating(self, uid, mid, rating): self.user_rating[mid][uid] = rating ? Feb 17, 2014 at 3:43
  • It would be so much easier for everyone if you posted a sample data file Feb 17, 2014 at 3:47
  • The data file just has ratings contained in in this fashion (yes, there are occasions where the same users rated multiple things)--- uid::mid::Rating Feb 17, 2014 at 4:11

1 Answer 1

1

There is no magic special syntax for dictionaries of dictionaries. You just use the normal dictionary syntax twice to get to the values of the inner dicts.

self.user_rating is a dict.

You look up a subdict like this

self.user_rating[mid]  # the value here is a subdict

and so the value in the subdict is found by adding another key lookup

self.user_rating[mid][uid]

And of course to create an empty subdict

self.user_rating[mid] = {}

Then add a rating like this

self.user_rating[mid][uid] = rating

If the uid might already have a value, you can test for that

if uid in self.user_rating[mid]:
    self.user_rating[mid][uid] = rating
else:
    self.user_rating[mid] = {uid: rating}

When you've worked out these basics, you should read the docs on collections.defaultdict

4
  • It's probably also worth noting that you can't do self.user_rating[mid][uid] = rating if self.user_rating[mid] doesn't exist yet. Feb 17, 2014 at 4:00
  • Thank you. How come though when I declared it as self.user_rating[mid] = {} , when I try to use self.user_rating[mid][uid] = rating in a different function it does not work properly? Feb 17, 2014 at 4:02
  • @user3295674, can you post a self contained example that fails like that? Feb 17, 2014 at 4:27
  • @gnibbler, thank you for helping me understand this, I've updated the question with extra information. Feb 17, 2014 at 4:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.