Python - Quoting through a multidimensional dictionary

If I explain what I think I'm doing, I hope someone can explain what I'm wrong about.

I have the following dictionary:

ls = [{ 'The Wolf Gift (13)': { 'cover': 'V:\\Books\\Anne Rice\\The Wolf Gift (13)\\cover.jpg', 'author': 'Anne Rice', 'year': '1988' }, 'Mummy (14)': { 'cover': 'V:\\Books\\Anne Rice\\Mummy (14)\\cover.jpg', 'author': 'Anne Rice', 'year': '1989' }, }] 

First of all, is the above multi-dimensional dictionary? I want to make sure I'm talking about the right thing. Secondly, how do I get through it to get information at different levels. The dictionary is dynamically filled, so I do not know the keys before starting work.

I tried for book in ls and then book['cover'] , etc., but it does not seem to work. I need the title of the book, and then additional information for each book (cover, etc.). I am new to python. I come from PHP, and using arrays is my bread and butter, but python is killing me ....

thanks

+4
source share
4 answers

This is a list containing one dictionary. You can do something like:

 >>> books = ls[0] >>> for book, details in books.iteritems(): print book,'-->', details['cover'] ... Mummy (14) --> V:\Books\Anne Rice\Mummy (14)\cover.jpg The Wolf Gift (13) --> V:\Books\Anne Rice\The Wolf Gift (13)\cover.jpg 
+2
source

This is an example that can be used if ls contains more than one dictionary.

 for dic in ls: for key in dic: print 'Book Name: %s' % (key) for value in dic[key]: print '\t%s: %s' % (value, dic[key][value]) 

This will lead to the following conclusion:

 Book Name: Mummy (14) year: 1989 cover: V:\Books\Anne Rice\Mummy (14)\cover.jpg author: Anne Rice Book Name: The Wolf Gift (13) year: 1988 cover: V:\Books\Anne Rice\The Wolf Gift (13)\cover.jpg author: Anne Rice 

Or you can remove the final loop and access the keys like this:

 for dic in ls: for key in dic: print 'Book Name: %s' % (key) print 'Publish Year: %s' % dic[key]['year'] 

which will give the following result:

 Book Name: Mummy (14) Publish Year: 1989 Book Name: The Wolf Gift (13) Publish Year: 1988 
+3
source

ls is a list containing a dictionary. This dictionary contains keys, which are books, and meanings, dictionaries. Thus, you can access them as follows:

 for book in ls[0]: covername = ls[0][book]['cover'] print(covername) 

which prints:

 V:\Books\Anne Rice\The Wolf Gift (13)\cover.jpg V:\Books\Anne Rice\Mummy (14)\cover.jpg 

ls [0] refers to the first item in the list

[book] is that dict keys are repeated

['cover'] is an element that is read from the dict referenced by [book]

+2
source

OK, therefore, first of all, your dictionary is actually a list containing the dictionary (you have [] around its declaration). This can be a problem. Once you know this, it's pretty easy to get everything you want. For instance:

 for key in ls[0].keys(): print ls[0][key] 

Or, if you want to access detailed information about each book (contained in another dictionary in the most recent of each book):

 for bookKey in ls[0].keys(): print ls[0][bookKey]['cover'] print ls[0][bookKey]['author'] print ls[0][bookKey]['year'] 

Hope this helps. Perhaps think about getting rid of this list in all of this, make your life a little easier (one less index to use).

+2
source

Source: https://habr.com/ru/post/1496354/


All Articles