Home > Software engineering >  How would you iterate through a library where the index keys are given as integers, but are predefin
How would you iterate through a library where the index keys are given as integers, but are predefin

Time:10-20

I have a dictionary which looks like this and is already part of a constructor :

self.__books = {
                    1001: {'title': 'Introduction to Programming', 'author': 'Farell', 'copies': 2}, 
                    1002: {'title': 'Database Concepts', 'author': 'Morris', 'copies': 5}, 
                    1003: {'title': 'Object Oriented Analysis', 'author': 'Farell', 'copies': 4}, 
                    1004: {'title': 'Linux Operating System', 'author': 'Palmer', 'copies': 2}, 
                    1005: {'title': 'Data Science using Python', 'author': 'Russell', 'copies': 4}, 
                    1006: {'title': 'Functional Programming with Python', 'author': 'Babcock', 'copies': 6}
                    }

What I am trying to do is print the dictionary but in a string format ( for example -

     1001 : title: Introduction to Programming, author: Farell, copies: 2 (\n)
     1002 : .... ( Hope you get the idea)) 

As per the problem given, I have to create a property called books that gets the value of 'self.__books' so it can be displayed and referenced w/o changing the original value of 'self.__books' in the future, so that the original can be referenced, if needed ( I also have to practice adding and removing from the list hence the need for the property and not just an attribute - correct me if I misunderstood the idea please.) I have looked up a bunch of resources that explain how to iterate through a dictionary using the key,value method like so -

    def books(self):
         for (key,value) in self.__books : 
            return  "{0} : {1}".format(key,value)

And all I am getting is an error message that says the compiler 'cannot unpack non-iterable int object'

I do understand that the 1001 is an int, hence the problem when the process starts, but I cannot figure out how to reference the value of the item at that index when the key is an integer ( the key was already given to differentiate the index values - I do not know what such a key would be called i.e, like a 'index key' or 'indexer' maybe?) Maybe the formatting of the original dictionary 'self.__books' stops it somehow ?

Any help would be appreciated and thank you in advance for taking the time to read this.

CodePudding user response:

You must iterate by dict as:

for key in self.__books:
    return "{0} : {1}".format(key,self.__books[key])

CodePudding user response:

def books(self):
    for (key,value) in self.__books.items() : 
    return "{0} : {1}".format(key,str(value).replace('{','').replace('\'','').replace('}',''))

I think this will do the trick.

CodePudding user response:

Dict have keys() and items() methods for iteration. Here in your case the item inside the dict is another dict, try something like below:

for k in self.__books.keys():
  ls = self.__books[k]
  ss = ""
  i = 0
  for j in ls.keys():
    if i > 0:
      ss = ss   ", "
    ss = ss   str(j)   ": "   str(ls[j])
    i  = 1
    
  print("{0} : {1}".format(k, ss))
  • Related